简体   繁体   English

使用ng-repeat重新加载时未设置选择项目的内容

[英]Content of select items is not set when reloading using ng-repeat

I have a code that is able to create a pair of selects and input elements with ng-repeat upon pressing a button. 我有一个代码,可以在按下按钮时使用ng-repeat创建一对选择和输入元素。 The elements are defined in a seperate directive: 元素在单独的指令中定义:

<select ng-model="rule.parameter" ng-options="sel1.name for sel1 in selects1" ng-change="change(rule.parameter)"></select>

The data can be saved to a parameter $scope.mydata by pressing a button in the controller section: 通过按控制器部分中的按钮,可以将数据保存到参数$scope.mydata中:

<button ng-click="save(myselects)">Save data</button>

The js code is as follows: js代码如下:

$scope.save = function(filter) {
    $scope.mydata = JSON.stringify(filter, null, 2);    
};

Now suppose we have save our data from $scope.mydata in a file (see mydata.txt in Fiddle ). 现在,假设我们已将$scope.mydata中的数据保存到一个文件中(请参阅Fiddle中的 mydata.txt )。 When the user presses another button to get the data back, the elements are supposed to be generated and displayed again: 当用户按下另一个按钮以取回数据时,应该生成并再次显示元素:

<button ng-click="getData()">Get data</button>

Code in JavaScript to get the data back: JavaScript中的代码以获取数据:

$scope.getData = function() {
    $http({
      method: 'GET',
      url: 'mydata.txt'
    }).success(function(data) {
      if (data != null && data != "") {
        $scope.myselects = data;        
      }
    });
};

My problem is, that the elements in the select boxes are not set when I click that button. 我的问题是,当我单击该按钮时,未设置选择框中的元素。 The content of the input field is set but that of the two select boxes is not. 输入字段的内容已设置,但两个选择框的内容未设置。

Can anyone help me on this? 谁可以帮我这个事?

The complete Fiddle is here . 完整的小提琴在这里 Just click on the Get data button (there is some content in mydata.txt already). 只需单击“ 获取数据”按钮( mydata.txt中已经有一些内容)。

Thanx in advance! 提前感谢!

By default, ngModel watches the model by reference, not value. 默认情况下,ngModel通过引用而非值监视模型。

When you load the data from mydata.txt the model ( rule.parameter ) for the first select will be: mydata.txt加载数据时,第一个选择的模型( rule.parameter )将是:

{"name":"Item1","relation":"default"}

The array of options will contain an item that looks the same, but it will still be another object. 选项数组将包含一个看起来相同的项目,但它仍然是另一个对象。

For example, this will log false : 例如,这将记录为false

var object1 = {"name":"Item1","relation":"default"};
var object2 = {"name":"Item1","relation":"default"};

console.log(object1 === object2);

One solution is to use track by to instead identify the objects by a value of a property. 一种解决方案是使用track by ,而不是通过属性的值来标识对象。 Note that the value of the property you choose must be unique . 请注意,您选择的属性的值必须唯一

For example: 例如:

<select ng-model="rule.parameter" 
        ng-options="sel1.name for sel1 in selects1 track by sel1.name" 
        ng-change="change(rule.parameter)"></select>

To get the second select to work properly you need to build the filtered array not only when you change the selected value of the first select, but also upon initialization. 为了使第二选择正常工作,您不仅需要在更改第一选择的选定值时而且还要在初始化时构建过滤后的数组。

For example, add the following to the link function of dynamicSelectPair: 例如,将以下内容添加到dynamicSelectPair的链接函数中:

if (scope.rule.parameter) scope.change(scope.rule.parameter);

Demo: http://plnkr.co/edit/ANWhMcUh86MSeOi3WCV3?p=preview 演示: http : //plnkr.co/edit/ANWhMcUh86MSeOi3WCV3?p=preview

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

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