简体   繁体   English

如何在Angularjs中使用ng-repeat更改选定的索引项值?

[英]How to change selected index item value with ng-repeat in Angularjs?

This is my example code. 这是我的示例代码。 I'm trying long time but not able to figure out. 我正在尝试很长时间,但无法弄清楚。

app.controller('homeTabCtrl', function($scope) {

    $scope.foodTypes = [{
        'filterId': 1,
        'countryName': 'American',
        'foodIcon': 'img/icon-burger.png'
    }, {
        'filterId': 2,
        'countryName': 'Korean',
        'foodIcon': 'img/icon-processing.png'
    }];

    /* Discover live order example */
    $scope.fId = 1;
    $scope.fId.push('filterId': 'India');

});

Here i have selected the first index. 在这里,我选择了第一个索引。 I want to change country name for first index. 我想更改第一个索引的国家/地区名称。 How can i implement this one with controller itself. 我如何使用控制器本身实现这一目标。

  1. fId should be array if you want to push items in it 如果要在其中push项目,则fId应该为array
  2. To push object you need to use {} syntax. 要推送object您需要使用{}语法。

SYNTAX ERRORS 语法错误

Change: 更改:

$scope.fId = 1;

To

$scope.fId = []; // Notice [] Define blank array

Change 更改

$scope.fId.push('filterId':'India');

TO

$scope.fId.push({
    'filterId': 'India'
}); // Notice { and }

Example

$scope.foodTypes[0].filterId = 'India';

In order to change the existing object, you need to find it first. 为了更改现有对象,您需要首先找到它。

$scope.foodTypes.find(function(food) {
    return food.filterId === 1;
}).countryName = "India";

If you know that it's always the first object, then: 如果您知道它始终是第一个对象,则:

$scope.foodTypes[0].countryName = "India";

Will work just fine. 将正常工作。

Your index is stored in $scope.fId field and You want to change the collection object at that index. 您的索引存储在$ scope.fId字段中,并且您想在该索引处更改集合对象。 So use it as below, 因此,如下使用它,

$scope.foodTypes[$scope.fId].countryName = 'India';

Hope this will help. 希望这会有所帮助。

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

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