为什么我的控制器没有触发我的function.js脚本

时间:2015-04-07 13:16:06

标签: javascript html angularjs angularjs-controller

我有function.js脚本,其中包含onClickClear()onValidate()个功能,但它们根本没有触发。我在函数中注入loginCtrl但它仍然不会触发onClickClear()onValidate()函数。以下是function.js代码,用于清除文本框并添加验证以及我的main.cshtml

var app = angular.module('HelloWorld', []);
app.controller("loginCtrl", function ($scope) {
    $scope.onClickClear = function () {
        $scope.UsernameTxtBox = "";
        $scope.passwordTxtBox = "";
        $scope.rememberMeCheckBox = "";
    };

    $scope.onValidate = function () {
        return ($scope.UsernameTxtBox != "" && $scope.UsernameTxtBox != null) &&
        ($scope.passwordTxtBox != "" && $scope.passwordTxtBox != null);
    };
});

    <body ng-app="HelloWorld">
        <div ng-controller="loginCtrl">
            <div class="container">
                <div class="row">
                    <div class="col-xs-2 col-xs-push-5">
                        <h3>Login</h3>
                    <p>
                        Username:<br />
                        <input type="text" autofocus name="UsernameTxtBox" ng-model="UsernameTxtBox" ng-minlength="3" ng-maxlength="20" placeholder="Username" required>
                        <span style="color: red" ng-show="form1.UsernameTxtBox.$error.minlength">Userame Should Contain Atleast 3 Characters</span>
                        <span style="color: red" ng-show="form1.UsernameTxtBox.$error.maxlength">Limit Exceeded</span>
                    </p>
                    <p ng-bind="name"></p>
                    <p>
                        Password:<br />
                        <input type="password" name="passwordTxtBox" ng-model="passwordTxtBox" ng-minlength="3" ng-maxlength="20" placeholder="Password" required>
                        <span style="color: red" data-ng-show="form1.passwordTxtBox.$error.minlength">Password Should Contain Atleast 3 Characters</span>
                        <span style="color: red" data-ng-show="form1.passwordTxtBox.$error.maxlength">Limit exceeded</span>
                    </p>
                    <p ng-bind="surname"></p>
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" data-ng-model="rememberMeCheckBox" data-ng-disabled="!onValidate()">Remember me</label>
                        </div>
                        <button type="button" class="btn btn-success btn-success btn-sm" data-ng-click="onClickClear()" ng-disabled="!onValidate()">Clear</button>
                        <button type="button" class="btn btn-primary btn-sm" data-ng-click="onClickLogin()" ng-disabled="!onValidate()">Login</button>
                        <button type="button" class="btn btn-link btn-xs" data-ng-click="onClickOpen()">Not A Member? Register</button>
                    </div>
                </div>
            </div>
        </div>
    </body>

1 个答案:

答案 0 :(得分:2)

我不确定你做了什么,但示例是在代码片段中工作...它基本上是从你的代码剪切和粘贴我只是将body标签更改为div并添加用户名的两个输入框和密码。

&#13;
&#13;
var app = angular.module('HelloWorld', []);
app.controller("loginCtrl", function ($scope) {
    $scope.onClickClear = function () {
        $scope.UsernameTxtBox = "";
        $scope.passwordTxtBox = "";
        $scope.rememberMeCheckBox = "";
    };

    $scope.onValidate = function () {
        return ($scope.UsernameTxtBox != "" && $scope.UsernameTxtBox != null) &&
        ($scope.passwordTxtBox != "" && $scope.passwordTxtBox != null);
    };
});
&#13;
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

    <div ng-app="HelloWorld">
        <div ng-controller="loginCtrl">
            <div class="container">
                <div class="row">
                    <div class="col-xs-2 col-xs-push-5">
                        <h3>Login</h3>
                        <div class="form-group"><input class="form-control" ng-model="UsernameTxtBox" placeholder="Username"></div>
                        <div class="form-group"><input class="form-control" type="password" ng-model="passwordTxtBox" placeholder="Password"></div>
                        <div class="checkbox">
                            <label>
                                <input type="checkbox"  data-ng-model="rememberMeCheckBox" data-ng-disabled="!onValidate()">Remember me</label>
                        </div>
                        <button type="button" class="btn btn-success btn-success btn-sm" data-ng-click="onClickClear()" ng-disabled="!onValidate()">Clear</button>
                        <button type="button" class="btn btn-primary btn-sm" data-ng-click="onClickLogin()" ng-disabled="!onValidate()">Login</button>
                        <button type="button" class="btn btn-link btn-xs" data-ng-click="onClickOpen()">Not A Member? Register</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
&#13;
&#13;
&#13;