ng-model not binding to controller - angularjs

I am trying to bind the value of a text input to a model value, but the value remains undefined...
The HTML:
<div class="panel panel-default">
<div class="panel-body">
<form name=user-form class="form-horizontal" novalidate>
<label for="firstname" class="col-sm-2 control-label">Firstname</label>
<div class="col-sm-4">
<input type="text" ng-model="ctrl.formData.firstname" name="firstname" class="form-control" placeholder="Firstname"/>
</div>
<div class="col-md-offset-2">
<input type="submit" class="btn btn-primary" name="submit" value="Add" ng-click="ctrl.submit()">
</div>
</form>
</div>
</div>
AngularJS:
angular.module('app', ['ngRoute']);
angular.module('app').config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'pages/main.html',
controller: 'MainController',
controllerAs: 'ctrl'
})
.when('/user', {
templateUrl: 'pages/user.html',
controller: 'UserController',
controllerAs: 'ctrl'
});
});
angular
.module('app')
.controller('UserController', ['$http', '$scope', function($http, $scope) {
var self = this;
self.formData = {};
self.submit = function() {
$http.post('http://localhost:50211/api/user/add',
JSON.stringify(self.getUserData()))
.success(function() {
console.log('Success');
})
.error(function() {
console.log('Failed');
});
};
self.getUserData = function() {
return {
FirstName: self.formData.firstname
};
};
}]);
The data object for the ajax request always contains 'Firstname: undefined', I'm guessing I'm missing something simple but cant see it.

Related

ui bootstrap modal Unknown provider $uibModalInstanceProvider

I am stuck again with my angular ui-bootstrap modal window.
I have header which contains
<a ng-click="openLoginModal()">Zaloguj</a>
and have controller named headerCtrl.js
app.controller('HeaderCtrl', ['$scope', '$uibModal', 'ApiService', function($scope, $uibModal, ApiService) {
"use strict";
$scope.allGames = ApiService.GetGames();
$scope.openLoginModal = function(size, parentSelector) {
var parentElem = parentSelector ?
angular.element($document[0].querySelector('.modal-login' + parentSelector)) : undefined;
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
ariaLabelledBy: 'modal-header',
ariaDescribedBy: 'modal-body',
templateUrl: 'app/views/modals/LoginModal.html',
controller: 'LoginModalCtrl',
controllerAs: '$ctrl',
backdropClass: "modal-backdrop",
size: size,
backdrop: true,
appendTo: parentElem,
resolve: {
items: function() {
return $scope.items;
}
}
});
};
}]);
After I click "Zaloguj" which starts openLoginModal method modal shows but doesn't have any content and in cosole shows error:
Error: [$injector:unpr] Unknown provider: $uibModalInstanceProvider <- $uibModalInstance <- LoginModalCtrl
LoginModalCtrl is induced in headerCtrl at line 14:
controller: 'LoginModalCtrl',
LoginModalCtrl.js code:
app.controller('LoginModalCtrl', ['$scope', '$location', '$uibModalInstance', 'authenticationService', function($scope, $location, $uibModalInstance, authenticationService) {
var $ctrl = this;
$ctrl.modalInstance = null;
$ctrl.animationsEnabled = true;
$ctrl.cancel = function() {
$uibModalInstance.dismiss('cancel');
};
$ctrl.login = function() {
authenticationService.login($ctrl.loginData)
.then(function(data) {
$uibModalInstance.close();
$location.url("/");
}, function(error) {
});
};
}]);
EDIT
Modal template code is:
<div class="modal-login">
<script type="text/ng-template" id="loginModalContent.html">
<div id="ModalLog" tabindex="-1" role="dialog" aria-labelledby="Zaloguj się">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" aria-label="Close" ng-click="$ctrl.cancel();"><span aria-hidden="true">×</span></button>
<h2 class="modal-title" id="myModalLabel">Zaloguj się do accTrader.com</h2>
</div>
<div class="modal-body">
<form method="post" name="loginData">
<label for="login">Login:</label>
<input type="text" placeholder="login123" ng-model="$ctrl.loginData.login" name="login" title="Wpisz swój login, użyj 6 do 20 znaków!" required />
<label for="password">Hasło:</label>
<input type="password" placeholder="hasło" ng-model="$ctrl.loginData.password" name="password" title="Wpisz swój login, użyj 8 do 20 znaków!" required />
<div class="row forget-valid">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
Zapomniałem hasła
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 text-right">
<span ng-show="(loginData.login.$touched && loginData.login.$invalid)||(loginData.password.$touched && loginData.password.$invalid)" class="error">
Pola nie mogą zostać puste!
</span>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default close-button" ng-click="$ctrl.cancel()">Zamknij</button>
<button type="button" ng-disabled="loginData.login.$invalid || loginData.password.$invalid" class="btn btn-primary btn-md" ng-click="$ctrl.login()">Zaloguj się</button>
</div>
</div>
</div>
</script>
help
You need to include $uibModal as a parameter to the controller function just like you have done for $scope.

angular.js:13920 Error: [$injector:unpr] Unknown provider: $uibModalInstanceProvider <- $uibModalInstance <- empCtrl

I am using a ui-bootstrap modal to submit a form, my modal is loading fine but my "Cancel" button doesn't work properly and i am getting the above mentioned error message.
my app.js
var myApp = angular.module('myApp', ['ui.bootstrap']);
myApp.controller('empCtrl', [
'$scope', '$http', '$uibModal', '$uibModalInstance', function ($scope, $http, $uibModal, $uibModalInstance) {
$scope.employees = "";
$http.get("/Home/GetEmployees").success(function(result) {
$scope.employees = result;
}).error(function(result) {
alert("error");
});
$scope.saveData = function(employee) {
$http.post("/Home/AddEmployee", { employee: employee }).sucess(function(result) {
alert(result);
}).error(function(result) {
alert(result);
});
}
$scope.showCreateEmployeeForm = function() {
$uibModal.open(
{
templateUrl: "app/employeeTemplate/employeeAdd.html",
controller: 'empCtrl'
});
$uibModalInstance.cancel();
}
$scope.cancelForm= function() {
$uibModalInstance.dismiss();
}
}
]);
my modal in html
<div class="container" ng-controller="empCtrl">
<div class="modal-header">
<h1>Create Employee</h1>
</div>
<div class="modal-body">
<div class="form-group">
<div class="col-lg-4 input-group">
<label>Name</label>
</div>
<div class="col-lg-6">
<input type="text" ng-model="employee.name" />
</div>
</div>
<div class="form-group">
<div class="col-lg-4 input-group">
<label>Address</label>
</div>
<div class="col-lg-6">
<input type="text" ng-model="employee.address" />
</div>
</div>
</div>
<div class="modal-footer">
<div class="col-sm-offset-3 col-sm-9" >
<input type="submit" value="Save" name="Save" ng-click="saveData(employee)" />
<input type="submit" value="Cancel" class="btn btn-success" ng-click="cancelForm()" />
</div>
</div>
</div>
can someone please help me to figure out what's going on with my code.
Cheers!!!
There is something weird on the controller. For the modal and for place where you init the modal you use the same controller. Your code should be in this format:
var myApp = angular.module('myApp', ['ui.bootstrap']);
myApp.controller('empCtrl', [
'$scope', '$http', '$uibModal', function ($scope, $http, $uibModal) {
$scope.employees = "";
$http.get("/Home/GetEmployees").success(function(result) {
$scope.employees = result;
}).error(function(result) {
alert("error");
});
$scope.showCreateEmployeeForm = function() {
$uibModal.open(
{
templateUrl: "app/employeeTemplate/employeeAdd.html",
controller: function($scope, $http, $uibModalInstance){
$scope.saveData = function(employee) {
$http.post("/Home/AddEmployee", { employee: employee }).sucess(function(result) {
alert(result);
}).error(function(result) {
alert(result);
});
}
$scope.cancelForm= function() {
$uibModalInstance.dismiss();
}
}
resolve: {
return $scope.employees;
}
});
}
}
]);

Angularjs Login Application with Cookies

I try to create a login app using Angularjs that storing information in $cookies. Once the user is logged in, they don't have to log in again:
This is login form:
<form action="/" id="login">
<div class="form-group">
<label for="username">Username</label>
<input type="text" name="username" id="username" class="form-control" ng-model="username" required />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" ng-model="password" required />
</div>
<div class="form-actions">
<button id="submit_btn" type="button" ng-click="submit()" class="btn btn-danger">Login</button>
</div>
</form>
This is the app.js
var app = angular.module('mailApp', ['angular.chosen','ngRoute','ngCookies'], function(){});
app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider.
when('/', {
templateUrl: 'login.html',
}).
when('/logs/', {
templateUrl: 'index.html',
controller: 'mailController',
}).
otherwise({
redirectTo: '/'
});
}]);
app.controller('mailController', function($scope, $http,$routeParams,$location,domainService, $rootScope, $location, $cookies)
{
$rootScope = false;
$scope.submit = function (info) {
if($scope.username == '123' && $scope.password == '123') {
$rootScope.loggedIn = true;
$location.path('/logs/');
}
else{
alert('Wrong authentication!');
}
}
//...
}
But I am new to angularjs and not really got to $cookies.
Any help would be appreciated!
Thanks, guys!

Angular doesn't Open Correct Modal

I am trying to open edit page:
<div id="edit" class="span6" ng-controller="BrandsCtrl">
<script type="text/ng-template" id="editPage">
<div class="row-fluid sortable">
<div class="box span12" ng-app="myApp">
<div class="box-content">
<form class="form-horizontal" action='/admin.brands/update' data-toggle="validate" method="post">
<input type="hidden" name="brandid" value="{{brand.brandid}}"/>
<div class="section-heading"></div>
<div class="control-group">
<label class="control-label" for="selectError"><tags:label text="name"/> *</label>
<div class="controls">
<input name="name" value="{{brand.name}}" required/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="selectError"><tags:label text="isactive"/> </label>
<div class="controls">
<input type="checkbox" ng-checked="post.isactive" name="isactive" value="1"/>
</div>
</div>
<div class="form-actions">
<tags:label text="close"/>
<button type="submit" class="btn btn-ext-darkblue btn-disable-on-submit" ><tags:label text="save"/></button>
</div>
</form>
</div>
</div>
</script>
</div>
by clicking button:
<a class="btn btn-ext-darkblue btn-ext-darkblue savestockbtn" ng-click="open()"><tags:label text="edit"/></a>
but when I click it, current page is opened as modal.
How can I show editPage as modal ? What is wrong that I did ? How can I fix this code ?
here is my controllers:
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller("BrandsCtrl", function($scope, $http, $controller) {
$http.get('http://localhost/admin.brands/getJSONDataOfSearch').
success(function(data, status, headers, config) {
$scope.brands = data;
}).
error(function(data, status, headers, config) {
});
angular.extend(this, $controller("BrandCtrl", {$scope: $scope}));
});
app.controller("BrandCtrl", function($scope, $http, $modal) {
$http.get('http://localhost/admin.brands/getJsonBrandAndEdit?brandid=1').
success(function(data, status, headers, config) {
$scope.brand = data;
}).
error(function(data, status, headers, config) {
});
$scope.animationsEnabled = true;
$scope.open = function (size) {
var modalInstance = $modal.open({
animation: $scope.animationsEnabled,
templateUrl: 'editPage',
size: size,
resolve: {
item: function () {
return $scope.brand;
}
}
});
}});

Couldn't navigate from one page to another

I am new to angularjs...i am trying to navigate from login page to next page using router functionality....My login page is as follows
<body>
<div id='content' ng-app='myApp' ng-controller='LoginController'>
<div class="container">
<form class="form-signin" role="form" ng-submit="login()">
<h3 class="form-signin-heading">Login Form</h3>
<span><b>Username :</b>
<input type="username" class="form-control" ng-model="user.name" required>
</span>
</br></br>
<span><b>Password :</b>
<input type="password" class="form-control" ng-model="user.password" required>
</span>
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<button class="btn btn-lg btn-primary btn-block" type="submit" >
<b>Sign in</b></button>
{{nameis}}
</form>
</div> <!-- /container -->
</div>
<div ng-view></div>
</body>
my app.js file is as follows
'use strict';
//Define Routing for app
var applog = angular.module('myApp',[]);
applog.config(['$routeProvider', '$locationProvider',
function($routeProvider,$locationProvider) {
$routeProvider
.when('/login', {
templateUrl: '/navAng.html',
controller: 'LoginController'
})
.otherwise({
redirectTo: '/navAng.html'
});
$locationProvider.html5Mode(true); //Remove the '#' from URL.
}]);
and my controller js file is as follows
applog.controller("LoginController", function($scope, $location,$window){
$scope.nameis = "Test";
$scope.go = function ( path ) {
$location.path( path );
};
$scope.login=function()
{
var username=$scope.user.name;
var password=$scope.user.password;
if(username=="admin" && password=="admin")
{
$location.path( "/navAng.html" );
}
else
{
$scope.message="Error";
$scope.messagecolor="alert alert-danger";
}
}
});
Honestly i dont know where i am going wrong...the problem is i am getting the url of the page correctly in the url tab but i need to press enter to go to that page...i mean the page is not getting refreshed it just changes the existing url and just sits in the same page(i.e login page)....can someone plz help....
Add angular-route library too.In latest angular versions, it is available as a separate module.So, you need to inject 'ngRoute' for using $routeProvider service
index.html:
<html ng-app='myApp'>
<head>
<script src="angular.js"></script>
<script src="angular-route.js"></script>
</head>
<body>
<div ng-view></div>
<script type="text/javascript">
'use strict';
//Define Routing for app
var applog = angular.module('myApp', ['ngRoute']);
applog.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: '/login.html',
controller: 'LoginController'
})
.when('/navAng', {
templateUrl: '/navAng.html',
controller: 'navAngController'
})
.otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true); //Remove the '#' from URL.
}]);
applog.controller("LoginController", function($scope, $location, $window) {
//$scope.nameis = "Test";
$scope.go = function(path) {
$location.path(path);
};
$scope.login = function() {
var username = $scope.user.name;
var password = $scope.user.password;
if (username == "admin" && password == "admin") {
$location.path("/navAng");
} else {
$scope.message = "Error";
$scope.messagecolor = "alert alert-danger";
}
}
});
// next page controller
applog.controller("navAngController", function($scope, $location, $window) {
$scope.msg= "Success";
});
</script>
</body>
</html>
Use separate html for login. Because, you are using ng-view in index.html
Login.html:
<div id='content' ng-controller='LoginController'>
<div class="container">
<form class="form-signin" role="form" ng-submit="login()">
<h3 class="form-signin-heading">Login Form</h3>
<span><b>Username :</b>
<input type="username" class="form-control" ng-model="user.name" required>
</span>
<span><b>Password :</b>
<input type="password" class="form-control" ng-model="user.password" required>
</span>
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<button class="btn btn-lg btn-primary btn-block" type="submit" >
<b>Sign in</b>
</button>
{{nameis}}
</form>
</div> <!-- /container -->
</div>
This is another page to navigate, when login and password is correct
navAng.html:
<div> {{msg}} </div>

Resources