简体   繁体   English

在文本$ scope变量中定义的动态字符串ng-repeat

[英]dynamic string ng-repeat that define in text $scope variable

How to define dynamic ng-repeat in $scope variable like this: 如何在$ scope变量中定义动态ng-repeat ,如下所示:

$scope.radioOptions =[{value:1,name:'radio1'},{value:2,name:'radio2'}];
$scope.item = {
    model: "radio", 
    radioOptions:'opt in radioOptions',
    optValue:'value',
    optLabel:'name'
}

and use it in html: 并在html中使用它:

<div class="radio radio-info radio-inline" ng-repeat="item.radioOptions">
      <input type="radio" id="{{$index}}" ng-value="item.optValue" name="{{item.model}}">
       <label for="{{$index}}">{{item.optLabel }}</label>
 </div>

But error me 但是误会我

Error: [ngRepeat:iexp] http://errors.angularjs.org/1.4.8/ngRepeat/iexp?p0=item.radioOptions 错误:[ngRepeat:iexp] http://errors.angularjs.org/1.4.8/ngRepeat/iexp?p0=item.radioOptions

Now, How to create dynamic ng-repeat with this specification? 现在,如何使用此规范创建动态ng-repeat

Try this to generate radio dynamically, 尝试以此动态生成无线电,

  $scope.radioOptions =[{value:1,name:'radio1'},{value:2,name:'radio2'}];

  $scope.item = {
      model: "radio", 
      radioOptions:$scope.radioOptions,
      optValue:'value',
      optLabel:'name'
  }

and in html: 并在html中:

<div class="radio radio-info radio-inline" ng-repeat="option in item.radioOptions">
     <input type="radio" id="{{$index}}" ng-value="option[item.optValue]" name="{{option[item.model]}}">
     <label for="{{$index}}">{{option[item.optLabel]}}</label>
</div>

I solved problem with function that get name of array and then return array collection: 我解决了使用数组名称然后返回数组集合的函数的问题:

$scope.radioOptions =[{value:1,name:'radio1'},{value:2,name:'radio2'}];
$scope.item = {
    model: "radio", 
    radioOptions:'radioOptions',
    optValue:'value',
    optLabel:'name'
}

// get array collection by name
$scope.getCollection = function (name) {
        return $scope[name];
    };

and change html to: 并将html更改为:

<div class="radio radio-info radio-inline" ng-repeat="opt in getCollection(item.radioOptions)">
      <input  type="radio" id="{{$index}}" ng-value="opt[item.optValue]" name="{{item.model}}">
      <label for="{{$index}}">{{opt[item.optLabel] }}</label>
</div>

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

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