当我更改下拉列表中的值时,我的optionType
未获得更新。
<select ng-model="optionType" ng-options="op.option for op in ops" ng-change="optionChanged()"></select>
我使用ng-change
来检查值的变化。我在optionChanged()
的顶部放置了一个断点,并且在调用函数时看到optionType
的值没有改变。
.controller('uploadCtrl', function($scope) {
$scope.ops = [
{ option: "Report" },
{ option: "Support" },
{ option: "Learn" }
] ;
$scope.optionType = $scope.ops[0];
$scope.optionChanged = function() {
};
})
答案 0 :(得分:1)
尝试按照它将在控制台中打印所选值
$scope.optionChanged = function()
{
console.log($scope.optionType.option);
};
答案 1 :(得分:1)
尝试使用ng-selected
:
HTML
<select ng-model="optionType" ng-options="op.option for op in ops" ng-selected="optionSelected(optionType)"></select>
JS
.controller('uploadCtrl', function($scope) {
$scope.ops = [
{ option: "Report" },
{ option: "Support" },
{ option: "Learn" }
] ;
$scope.optionType = $scope.ops[0];
$scope.optionSelected = function(option) {
$scope.optionType = option;
//if the above doesn't work, tell me your console values
console.log($scope.optionType); //or
alert($scope.optionType);
};
})
答案 2 :(得分:1)
我认为这是一个范围问题。使用$ parent绑定,如下所示:ng-model="$parent.optionType"
,或将变量放在对象中:
JS:
$scope.option = { optionType: $scope.ops[0] };
HTML:
<select ng-model="option.optionType" ng-options="op.option for op in ops" ng-change="optionChanged()"></select>
来源:https://github.com/angular/angular.js/wiki/Understanding-Scopes