我是角js的新手。我正在创建一些应用程序,我从下拉列表中选择一个值,并将所选值与ng-model关联。但是我发现ng模型实际上变为null而不是gettting一个值。 我的整个代码是:Angular JS ng模型在从下拉列表中选择变得无效
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
</head>
<body>
<div ng-app="app">
<div ng-controller="MyCtrl">
<h2>Select one Item from left and one Item from right</h2>
<select ng-model="leftmodel"
ng-change="ChangeOptions()"
ng-options="option as option.value disable
when option.disabled for option in left">
</select>
<select ng-model="rightmodel"
ng-change="ChangeOptions()"
ng-options="option as option.value disable when option.disabled for option in right">
</select>
</div>
</div>
<script>
var module = angular.module("app", []);
module.controller("MyCtrl",
function($scope) {
$scope.left = [{
"name": "apple",
"value": "Nice Apple",
"disabled": false
}, {
"name": "orange",
"value": "Yellow Orange",
"disabled": true
}, {
"name": "berry",
"value": "Blue Berry",
"disabled": false
}];
$scope.right = [{
"name": "apple",
"value": "Nice Apple",
"disabled": true
}, {
"name": "orange",
"value": "Yellow Orange",
"disabled": false
}, {
"name": "berry",
"value": "Blue Berry",
"disabled": false
}];
$scope.leftmodel = $scope.left[0];
$scope.rightmodel = $scope.right[1];
$scope.ChangeOptions = function() {
var size = $scope.left.length;
console.log(size);
console.log($scope.leftmodel);
console.log($scope.rightmodel);
for (var i = 0; i < size; i++) {
if ($scope.left[i].name === $scope.rightmodel.name) {
$scope.right[i].disabled = true;
} else {
$scope.right[i].disabled = false;
}
}
for (var i = 0; i < size; i++) {
if ($scope.right[i].name === $scope.leftmodel.name) {
$scope.left[i].disabled = true;
} else {
$scope.left[i].disabled = false;
}
}
};
}
);
</script>
</body>
</html>
我也有一个Codpen 在控制台我得到以下错误:
3 Object { $$hashKey: "object:5", disabled: false, name: "berry", value: "Blue Berry" } Object { $$hashKey: "object:7",
disabled: false, name: "orange", value: "Yellow Orange" } 3 null Object { $$hashKey: "object:7", disabled: true, name: "orange", value: "Yellow Orange" } "TypeError: Cannot read property 'name' of null at ChildScope.$scope.ChangeOptions (pen.js:87:58) at fn (eval at compile (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:15156:15), :4:159) at ChildScope.$eval (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:17972:28) at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:25711:13 at Object. (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28536:9) at forEach (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:357:20) at Object.$$writeModelToScope (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28534:5) at writeToModelIfNeeded (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28527:14) at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28521:9 at validationDone (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28446:9)" 3 null null "TypeError: Cannot read property 'name' of null at ChildScope.$scope.ChangeOptions (pen.js:79:58) at fn (eval at compile (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:15156:15), :4:159) at ChildScope.$eval (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:17972:28) at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:25711:13 at Object. (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28536:9) at forEach (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:357:20) at Object.$$writeModelToScope (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28534:5) at writeToModelIfNeeded (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28527:14) at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28521:9 at validationDone (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:28446:9)" Do anyone have any idea why this is happening. What i want to do is select a value from left drop down menu and dont allowing the user to select the same value from other drop down list. That meansif a user selects some value from left select box he will not be able to select that from right select box and vice versa.
不过我没有收到什么是我的代码,并使得它的工作 –
@MdJohirulIslam你的条件语句是向后什么神奇的变化在你的代码的问题。在原始代码中,如果左侧匹配,则禁用右侧,反之亦然。这会导致相反的下拉列表突然无效,导致其更改,导致另一个摘要循环,现在会导致第一个下拉列表也设置为无效值。如果左侧匹配,此代码将更改左侧,如果右侧匹配,则代码右侧匹配,这将起作用。 – Claies
@Claies谢谢,很好解释,这里是你如何看到这种情况发生和查看当模型由于这个变为null .https://codepen.io/anon/pen/YNybed – Deep