简体   繁体   English

AngularJS <select>不能在对象中保存价值?

[英]AngularJS <select> not saving value in object?

I have this select element in a row (of which there can be a few) which represents an item. 我在代表一个项目的行中有这个选择元素(可以有几个)。 But the scope for manufacturer does not update when this is selected. 但是,选择此选项后,制造商的范围不会更新。 The default for the item's manufacturer attribute is null (since when it's created - it no manufacturer has been selected yet) 该项目的制造商属性的默认值为null(自创建以来-尚未选择任何制造商)

<select ng-model="manufacturer" ng-options="manufacturers.name for manufacturers in manufacturers" ng-change="change()" class="ng-valid ng-dirty">
<option value="" selected="selected" class=""> --Select a Manufacturer--</option>
<option value="0">Choice1</option>
<option value="1">Choice2</option>
<option value="2">Choice3</option>
</select>

I guess my question is - how do I get the manufacturer attribute in the item to update with the select box? 我想我的问题是-如何在项目中获取制造商属性以使用选择框进行更新?

Thanks! 谢谢!

If it helps - here's the scope of the cams item: 如果有帮助-这是cams项目的范围:

$scope.cams = [
    {channels:1, manufacturer:null},
    {channels:1, manufacturer:null}
    ];

Turns out ng-model="manufacturer" should have been ng-model="cam.manufacturer" : 原来ng-model="manufacturer"应该是ng-model="cam.manufacturer"

<select ng-model="cam.manufacturer" ng-options="manufacturers.name for manufacturers in manufacturers" ng-change="change()" class="ng-valid ng-dirty">
<option value="" selected="selected" class=""> --Select a Manufacturer--</option>
<option value="0">Choice1</option>
<option value="1">Choice2</option>
<option value="2">Choice3</option>
</select>

Now it works perfectly. 现在,它可以完美运行了。

You could use this: 您可以使用此:

HTML
<selectmultiple ng-model="selectedValues">
    <option ng-repeat="lang inLanguages" value="{{lang.name}}" ng-click="selected(selectedValues)">{{lang.name}}</option>
</select>

angularjs: angularjs:

$scope.selected = function(nowSelected){
    $scope.selectedValues = [];
    if( ! nowSelected ){
        return;
    }
    angular.forEach(nowSelected, function(val){
        $scope.selectedValues.push(val);
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM