javascript 控制器功能没有在 ng-click 上被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20935697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Controller Function is not getting called on ng-click
提问by Vishwajeet Vatharkar
This is my View Code
这是我的查看代码
<div ng-controller="signupCtrl">
<ul class="list-group" >
<li class="list-group-item">
<div class="form-group">
<input type="text" ng-model="signupCtrl.firstName">
</div>
...
</div>
<div class="form-group">
<div class="pull-right">
<button ng-click="signupCtrl.signupUser()">Register</button>
</div>
</div>
</li>
</ul>
</div>
Update- This is my Controller Code ##
更新-这是我的控制器代码##
someAppControllers.controller('signupCtrl', [
'$window',
'$scope',
'HttpReqHandlerService',
'$location',
'localStorageService'],
function($window, $scope, HttpReqHandlerService,
$location, localStorageService) {
$scope.signupUser=function signupUser() {
alert("hello");
}]);
The button is not calling signupUser function in my controller
该按钮没有在我的控制器中调用 signupUser 函数
回答by Satpal
Use $scope.signupUser
instead of this.signupUser
使用$scope.signupUser
代替this.signupUser
Change you code as
将您的代码更改为
someAppControllers.controller('signupCtrl', ['$window', '$scope',
function ($window, $scope) { // Here you have to define function an pass window and scope
$scope.signupUser = function signupUser() {
alert("hello");
};
}
]);
Additionally, You have syntax error.
此外,您有语法错误。
HTML
HTML
Instead of
代替
<input type="text" ng-model="signupCtrl.firstName">
<button ng-click="signupCtrl.signupUser()">Register</button>
Use
利用
<input type="text" ng-model="firstName">
<button ng-click="signupUser()">Register</button>
回答by calebboyd
You've written your markup as though you used the controller as
syntax. To make it work just change your ng-controller="signupCtrl"
to ng-controller="signupCtrl as signupCtrl"
;
您编写的标记就像使用了controller as
语法一样。要使其工作,只需将您更改ng-controller="signupCtrl"
为ng-controller="signupCtrl as signupCtrl"
;