角度问题中的复选框

时间:2015-11-02 13:28:06

标签: angularjs checkbox

我定义了一个像这样的角度的复选框:

<input type="checkbox" id="rememberMe" name="rememberMe" ng-model="model.rememberMe" value="1" />

在我的controller我有:

  app.controller('loginController', function ($http, $scope) {

    $scope.login = function () {
        $scope.model.rememberMe = $scope.rememberMe == undefined ? false : true;       
        $http.post("/Accounts/UserLogin", $scope.model).success(function (response) {
            $scope.message = response;
        });
    }
});

但是在控制台中我有一个错误:Cannot set property 'rememberMe' of undefined,有什么问题?

3 个答案:

答案 0 :(得分:2)

您无法直接定义

$scope.model.rememberMe
//$scope.model is not defined (=undefined), therefore you get :
//Cannot set property 'rememberMe' of undefined

你必须这样做

$scope.model = {};  //define object "model"
$scope.model.rememberMe = ... //set property rememberMe of "model"

答案 1 :(得分:0)

我认为你的控制器应该是:

 app.controller('loginController', function ($http, $scope) {
    $scope.model = { rememberMe: false };
    $scope.login = function () {
        $scope.model.rememberMe = $scope.rememberMe == undefined ? false : true;       
        $http.post("/Accounts/UserLogin", $scope.model).success(function (response) {
            $scope.message = response;
        });
    }
});

我希望这有帮助。

答案 2 :(得分:0)

也许您忘了申报$ scope.model。

首先:

  

$ scope.model = {};

后:

  

$ scope.model.rememberMe = $ scope.rememberMe == undefined? false:是的;