我看过一些相关的帖子但在我的情况下没有成功,即使有相同的代码。
我有选择,如下所示:
<div class="form-group">
<label for="col">Visible: </label>
<select class="custom-select" id="col"
ng-options="visible as visible.name for visible in visibility"
ng-model="selected_visible_product"
ng-change="update_product_list()"
style="width:140px">
<option>Visibility...</option>
</select>
<label>{{selected_visible_product}}</label>
</div>
可见性数组的定义如下:
$scope.visibility = [{ID:0, name:'Disabled'},{ID:1, name:'Enabled'}];
首次打印 selected_visible_product 的值时,它会显示正确的值,但是当我更改选择选项时,update_product_list()
会再次打印 selected_visible_product 的实际值并且显示相同。
这就是update_product_list()打印的内容(无论你选择什么):
对象{ID:0,名称:&#34;已禁用&#34;,$$ hashKey:&#34;对象:140&#34;}
update_product_list()
只需在控制台中打印 selected_visible_product
$scope.update_product_list = function() {
console.info($scope.selected_visible_product.ID);
}
发生了什么?
更新:
答案 0 :(得分:0)
以下是您的代码的工作代码段。它似乎工作正常。有什么不同?
angular.module('myapp', [])
.controller('mycontroller', ['$scope', function mycontroller($scope) {
$scope.visibility = [{ID:0, name:'Disabled'},{ID:1, name:'Enabled'}];
$scope.update_product_list = function() {
alert($scope.selected_visible_product.name);
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="mycontroller">
<div class="form-group">
<label for="col">Visible: </label>
<select class="custom-select" id="col"
ng-options="visible as visible.name for visible in visibility"
ng-model="selected_visible_product"
ng-change="update_product_list()"
style="width:140px">
<option>Visibility...</option>
</select>
</div>
</div>