ng-options更改AngularJS

时间:2016-02-21 02:23:28

标签: angularjs

当我更改下拉列表中的值时,我的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() {

    };
})

3 个答案:

答案 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