Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在ng repeat中生成随机模型名称_Angularjs_Angularjs Scope_Angularjs Model - Fatal编程技术网

Angularjs 在ng repeat中生成随机模型名称

Angularjs 在ng repeat中生成随机模型名称,angularjs,angularjs-scope,angularjs-model,Angularjs,Angularjs Scope,Angularjs Model,在angular.js项目中,我有一个包含输入字段的循环 <input type="text" ng-model="myColour"> <div ng-repeat="c in colors"> <input type="text" ng-model="colour"> <a href ng-click="asd(colour)">Click</a> </div> 这给了我 colour input fi

在angular.js项目中,我有一个包含输入字段的循环

<input type="text" ng-model="myColour">
<div ng-repeat="c in colors">
    <input type="text" ng-model="colour">
    <a href ng-click="asd(colour)">Click</a>
</div>
这给了我

colour input field data
my colour input field data
undefined 
在这里,我无法访问“颜色”模型,如果它在视图中重复。所以我尝试生成随机模型名(将c.id与模型中的颜色连接起来),我尝试了几种方法来实现这一点,但没有成功

是否有方法生成随机ng模型名称


有没有办法访问其“单击”链接被单击的输入字段的模型?

尝试以下方法:

<div ng-repeat="c in colors">
    <input type="text" ng-model="c.colour">
    <a href ng-click="asd(c.colour)">Click</a>
</div>
<div ng-repeat="c in colors">
    <input type="text" ng-model="c.colour">
    <a href ng-click="asd(c.colour)">Click</a>
</div>
// your collection
$scope.colors = [
    {id: 1, name: 'black', shade: 'dark'},
    {id: 2, name: 'white', shade: 'light'},
    {id: 3, name: 'red', shade: 'dark'},
    {id: 4, name: 'blue', shade: 'dark'},
    {id: 5, name: 'yellow', shade: 'light'}
];

// add a new key called 'colour' on your colors which will be the model
angular.forEach($scope.colors, function(value, key){
   $scope.colors[key]['colour'] = ""; // match node in html
});