Angularjs:当ng模型更新时,选择不更新

Angularjs: select not updating when ng-model is updated

本文关键字:更新 选择 模型 ng Angularjs      更新时间:2023-09-26

我创建了以下示例,这样您就可以准确地看到发生了什么:http://jsfiddle.net/8t2Ln/101/

如果我使用ng选项,也会发生同样的情况。我有不同的理由这样做,但为了简化示例,省略了这一部分。

正如您所看到的,默认情况下它有两个选项。我在select旁边显示ng模型的选定值,这样你就可以看到它是什么。当你使用顶部添加第三个选项时,它会将值设置为新选项的值,正如select旁边显示的ng模型值所证明的那样,但select本身不会改变以显示正确的选定值。

以下是链接处的示例代码:

var testApp = angular.module('testApp', ['ngRoute']);
testApp.controller('Ctrl', function ($scope) {
    $scope.newInput = '';
    $scope.inputDevice = [
        {
            value: '1',
            label: 'input1'
        },
        {
            value: '2',
            label: 'input2'
        }
    ];
    $scope.selectedDevice = '';
    $scope.addType = function () {
        var newElem = {
            label: $scope.newInput,
            value: '3'
        };
        $scope.inputDevice.push(newElem);
        $scope.selectedDevice = newElem.value;
    };

});

这是html:

<div ng-app="testApp">
    <div ng-controller="Ctrl">
        <p>
            <input type="text" ng-model="newInput" />
            <br />
            <button ng-click="addType()">Add Type</button>
        </p>
        <select ng-model="selectedDevice">
            <option></option>
            <option ng-repeat="i in inputDevice" value="{{ i.value }}">{{ i.label }} - {{ i.value }}</option>
        </select>
        {{ selectedDevice }}</div>
</div>

这正是不应该使用ngRepeat来渲染选择选项的原因。您应该使用ngOptions

<select ng-model="selectedDevice" 
        ng-options="i.value as (i.label + '-' + i.value) for i in inputDevice">
    <option></option>
</select>

通常,请避免使用ngRepeat来渲染选择选项。至少有两个充分的理由。ngRepeat在每次迭代中创建单独的子作用域,这在选项标记的情况下是不需要的。另一个重要的警告是,使用ngRepeat,您只能将select绑定到字符串等基元,但不能用它将对象写入ngModel

下面是一个演示。

angular.module('demo', []).controller('DemoController', function($scope) {
    $scope.newInput = '';
    $scope.inputDevice = [
    	{value: '1', label: 'input1'}, 
        {value: '2', label: 'input2'}
    ];
    
    $scope.selectedDevice = '';
    $scope.addType = function() {
        var newElem = {
            label: $scope.newInput,
            value: Number($scope.inputDevice[$scope.inputDevice.length - 1].value) + 1
        };
        $scope.inputDevice.push(newElem);
        $scope.selectedDevice = newElem.value;
        $scope.newInput = '';
    };
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
<div ng-app="demo" ng-controller="DemoController">
    <form ng-submit="addType()">
        <input type="text" ng-model="newInput" />
        <button type="submit">Add Type</button>
    </form>
    <select ng-model="selectedDevice" ng-options="i.value as (i.label + ' - ' + i.value) for i in inputDevice">
        <option>Select</option>
    </select>
    {{ selectedDevice }}
</div>

问题是,由于您没有使用ng-options,因此在设置新的selectedDevice时,浏览器尚未完成渲染。如果您已启用ng-options,则可以使用此解决方法。使用$timeout包装$scope.selectedDevice = newElem.value;,以确保它在浏览器使用ng-repeat完成更改后运行。

我还添加了代码,以便在连续添加时增加下一个值,因为硬编码为"3"意味着即使添加了更多选项,也会继续选择第三个选项。

var testApp = angular.module('testApp', ['ngRoute']);
testApp.controller('Ctrl', function($scope, $timeout) {
  $scope.newInput = '';
  $scope.inputDevice = [{
    value: '1',
    label: 'input1'
  }, {
    value: '2',
    label: 'input2'
  }];
  $scope.selectedDevice = '';
  $scope.addType = function() {
    var last = Number($scope.inputDevice[$scope.inputDevice.length - 1].value) + 1;
    var newElem = {
      label: $scope.newInput,
      value: last.toString()
    };
    $scope.inputDevice.push(newElem);
    $timeout(function() {
      $scope.selectedDevice = newElem.value;
    }, 0);
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular-route.js"></script>
<div ng-app="testApp">
  <div ng-controller="Ctrl">
    <p>
      <input type="text" ng-model="newInput" />
      <br />
      <button ng-click="addType()">Add Type</button>
    </p>
    <select ng-model="selectedDevice">
      <option></option>
      <option ng-repeat="i in inputDevice" value="{{ i.value }}" ng-selelected="{{ selectedDevice == i.value }}">{{ i.label }} - {{ i.value }}</option>
    </select>
    {{ selectedDevice }}
  </div>
</div>

我也遇到了类似的问题,原因是我的键是一个数字,但当试图设置另一个值时,我发送了一个字符串。这种情况下的解决方法是强制将模型值设置为与键相同的类型。

例如:

<select ng-model="model" ng-options="option.{{ key }} as option.{{ label }} for option in options">
    <option value="">{{ emptyLabel }}</option>
</select>
if (scope.options.length > 0) {
    scope.keyType = typeof(scope.options[0][scope.key]);
}

if (scope.keyType == 'number') {
    scope.model = parseInt(newVal, 10);
} else {
    scope.model = newVal;
} 

当ng模型更新时,我也遇到了选择不更新的问题。我从一个函数中检索ng模型的值,该函数基于键值对从数组中提取对象。

在执行此操作时,返回的对象具有hashkey属性$$hashKey: "object:115"

当我使用angular.copy创建对象的副本时出现了问题,该副本去掉了这个"hashkey"属性,因此无法被选中。

在我重新组织代码后,为了在angular.copy后获得ng模型值,解决了这个问题

ConstructorReviewers: function (oItem) {
      this.PERSON_ID = oItem.PERSON_ID;
      this.CHAIR_PERSON = oItem.CHAIR_PERSON;
      /* // Commented this part and added it to EditItem function      
      this.oDepartment = CommonFactory.FindItemInArray(vm.oCommittee.arrDepartments, 'NAME', this.DEPARTMENT, 'item');
      */    
      this.EditItem = function () {
           vm.EditItemOriginal = this;
           angular.copy(this, vm.EditItem); // After this update the ng-model into vm.EditItem.oTitle object
           vm.EditItem.oTitle = CommonFactory.FindItemInArray(vm.oCommittee.arrTitles, 'TITLE', vm.EditItem.TITLE, 'item');
           vm.Popup.ShowPopup(true, 'new_edit', Constants.Mentoring.Popup.Edit);
      }
}

我也遇到了同样的问题。对我来说,解决问题的方法是将数字转换为字符串。示例:

$scope.selectedDevice = "" + newElem.value;