我有这个HTML:
<div>
<input type="checkbox" ng-model="EmailToUser" />
<input type="checkbox" ng-model="EmailToOwner" />
</div>
这在我的controller.js中:
$scope.EmailToUser = true;
$scope.EmailToOwner = false;
$scope.Save = function() {
if($scope.EmailToUser) {
alert("I'm supposed to email the user.");
}
if($scope.EmailToOwner) {
alert("I'm supposed to email the owner.");
}
}
这不起作用,当我单击复选框时,值true / false由于某种原因是常量。 EmailToUser
始终为true,无论复选框状态如何,EmailToOwner
始终为false。
但是,如果我将代码更改为:
<div>
<input type="checkbox" ng-model="EmailToUser.Value" />
<input type="checkbox" ng-model="EmailToOwner.Value" />
</div>
和controller.js:
$scope.EmailToUser = {};
$scope.EmailToUser.Value = true;
$scope.EmailToOwner = {};
$scope.EmailToOwner.Value = false;
$scope.Save = function() {
if($scope.EmailToUser.Value == true) {
alert("I'm supposed to email the user.");
}
if($scope.EmailToOwner.Value == true) {
alert("I'm supposed to email the owner.");
}
}
有效。为什么?我似乎无法弄清#1和#2之间的差异。 我是不是在范围内以相同的方式创建新对象并以两种方式分配true / false值?
答案 0 :(得分:0)
<强> controller.js(代码)强>
$scope.EmailToUser = true;
$scope.EmailToOwner = false;
$scope.save1 = function(EmailToUser,EmailToOwner){
if($scope.EmailToUser) {
alert("I'm supposed to email the user.");
}if($scope.EmailToOwner) {
alert("I'm supposed to email the owner.");
}
};
<强> controller.html 强>
<div>
<input type="checkbox" ng-model="EmailToUser" />
<input type="checkbox" ng-model="EmailToOwner" />
</div>
<button type="button" ng-click="save1(EmailToUser,EmailToOwner)">click</button>
此代码适用于我。存在一些范围问题。