AngularJS中的依赖性ng-重复

时间:2013-10-06 18:02:26

标签: angularjs angularjs-scope angularjs-ng-repeat

我有来自REST的以下数据结构:

  

scope.taglist =

     

[{name:“mylist”,tags:[“tag1”,“tag2”,“tag3”,...]},{name:   “mylist2”,标签:[“tag2.1”,“tag2.2”,“tag2.3”,...]}]

为了呈现对象的名称,我有以下html:

<div>
<select ng-model="tagNameSelection">
<option ng-repeat="tagObj in taglist" value="{{tagObj}}">{{tagObj.name}}</option>
</select>
</div>

<div class="tagdetails">
<!-- present the list of tags from tagNameSelection -->
</div>
  

现在我对如何呈现标签列表有点遗憾   个人对象。我能够以原始格式呈现数组(通过   在tagdetails div中粘贴{{tagNameSelection}}但是当我   尝试迭代那些使用ng-repeat angular给出错误   消息。

奇怪的是,当我将其中一个标签列表硬编码到控制器中的范围时,ng-repeat可以完美地工作。

1 个答案:

答案 0 :(得分:1)

也许你有点像这样:

<强> HTML

<div ng-controller="fessCntrl">
    <div>
        <select ng-model="tagNameSelection"            
         ng-options="tagObj as tagObj.name for tagObj in taglist"              
         ng-change="change(tagNameSelection)"></select>
    </div>

    <pre>{{tagNameSelection.tags|json}}</pre>

    <div class="tagdetails"> 
        <ul ng-repeat="tag in tagNameSelection.tags">
            <li>{{tag}}</li>
        </ul>    
    </div>
</div>

<强>控制器

var fessmodule = angular.module('myModule', []);

fessmodule.controller('fessCntrl', function ($scope) {



    $scope.change = function (value) {
    };

    $scope.taglist = [{
        name: "mylist",
        tags: ["tag1", "tag2", "tag3"]
    }, {
        name: "mylist2",
        tags: ["tag2.1", "tag2.2", "tag2.3"]
    }]

});

fessmodule.$inject = ['$scope'];

参见 Fiddle