【发布时间】:2016-01-28 17:49:17
【问题描述】:
在 Angular 1.3.x 上,我做了一个选择输入,它在 ng-change 上调用一个函数。周围有一个 ng-if 语句。
html
<div class="form-group" ng-if="property">
<label for="city-picker">in</label>
<select class='form-control focus input-sm' ng-model="cityIds" ng-options="city.id as city.name for city in cities" ng-change="getMetrics(cityIds)">
<option value="">all</option>
</select>
</div>
控制器
app.controller('MainCtrl', function($scope) {
$scope.cities = [{
name: "Amsterdam",
id: 1
},{
name: "paris",
id: 2
}]
$scope.property = true;
$scope.getMetrics = function(cityIds) {
console.log(cityIds);
console.log('$scope.cityIds is undefined: ', $scope.cityIds);
}
});
当我更改选择字段时,会使用正确的 $scope 更新调用 ng-change 函数,但是 $scope 不会更新。
查看以下插件:http://plnkr.co/edit/sQaLts5xyGBjThjdQJwM
当我删除 ng-if 语句时,$scope 会被 select 输入正确更新。有人可以解释这种行为吗?
【问题讨论】:
-
这里是解释:stackoverflow.com/questions/30787147/angularjs-ng-if-and-scopes。在 ng-model 路径中包含
.始终是一个好习惯。
标签: javascript angularjs angularjs-scope