控制器功能未在ng-click上调用

时间:2014-01-05 16:02:28

标签: javascript jquery angularjs

这是我的查看代码

<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>

更新 - 这是我的控制器代码##

someAppControllers.controller('signupCtrl', [
    '$window', 
    '$scope',     
    'HttpReqHandlerService',     
    '$location', 
    'localStorageService'],
    function($window, $scope, HttpReqHandlerService, 
             $location, localStorageService) { 
        $scope.signupUser=function signupUser() {
        alert("hello");
    }]);

该按钮未在我的控制器中调用signupUser函数

2 个答案:

答案 0 :(得分:8)

使用$scope.signupUser代替this.signupUser

将代码更改为

 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");
        };
    }
 ]);

此外,您还有语法错误。

HTML

而不是

<input type="text" ng-model="signupCtrl.firstName">
<button ng-click="signupCtrl.signupUser()">Register</button>

使用

<input type="text" ng-model="firstName">
<button ng-click="signupUser()">Register</button>

答案 1 :(得分:4)

您已经编写了标记,就好像使用了controller as语法一样。要使其有效,只需将ng-controller="signupCtrl"更改为ng-controller="signupCtrl as signupCtrl";