For some reason $scope.username
is not being passed to my controller. On click registerUser()
$scope.username
is undefined. I do have a scope and the function is running just no $scope.username.
For example:
HTML:
<input bs-form-control
type="text"
ng-model="username"
label="Username"
/>
<button class="btn btn-primary" ng-click="registerUser()">Register</button>
controller:
.controller("loginController", ['$scope',
function ($scope) {
$scope.registerUser = function(){
console.log($scope.username) // <=== undefined!
}
}])
My controller is passed using state:
.state('register', {
url: "/register",
templateUrl: "/static/html/profile/register.html",
controller: "loginController"
})
I think bs-form-control
creates a new isolated scope. What you should do is either use objects on your scope ( Does my ng-model really need to have a dot to avoid child $scope problems? ) or use the new "constroller as" syntax introducted in AngularJS 1.2. It works with ui-router too!
See this plunker: http://plnkr.co/edit/hkmMGbTtURSHDH84miiK?p=preview
.state('register', {
url: "/register",
templateUrl: "/static/html/profile/register.html",
controller: "loginController as loginCtrl"
})
<input bs-form-control type="text" ng-model="loginCtrl.username" label="Username" />
<button class="btn btn-primary" ng-click="loginCtrl.registerUser()">Register</button>
Use this
instead of $scope
:
.controller("loginController", [
function () {
this.registerUser = function(){
console.log(this.username);
}
}
])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.