Angular - {{}}不在ng-model属性标记中解析

时间:2014-01-23 17:17:44

标签: javascript angularjs

我正在尝试动态地向我的网站添加一些复选框,并根据其值进行过滤。

我在通过ng-model将每个过滤器添加到过滤器对象的部分失败了。

<div ng-repeat="filter in menu.filters">
    <label>{{filter.title}}</label>
    <input type="checkbox" ng-model="filters.filter{{$index+1}}" ng-true-value="{{filter.stub}}" ng-false-value='' />
</div>

然而,{{$index+1}}或实际上任何在ng-model属性中使用{{}}都会破坏整个循环。

我错过了什么?

这是该页面的完整代码:

<script type="text/ng-template" id="menu_item_renderer.html">
    {{category.title}}
    <ul class="global-menu-category-list">
        <li ng-repeat="category in category.categories | filter:filters" ng-include="'menu_item_renderer.html'"></li>
    </ul>
    <ul class="global-menu-link-list">
        <li ng-repeat="link in category.links | filter:filters">
            <a href="{{link.link}}">{{link.title}}</a>
        </li>
    </ul>
</script>

<form>
    <div ng-repeat="filter in menu.filters">
        <label>{{filter.title}}</label>
        <input type="checkbox" ng-model="filters.filter($index+1)" ng-true-value="{{filter.stub}}" ng-false-value='' />
    </div>
</form>

<div id="#global-menu" data-feature="globalMenu">
    <ul id="global-menu-categories">
        <li ng-repeat="category in menu.categories" ng-include="'menu_item_renderer.html'"></li>
    </ul>
</div>

目标是让复选框过滤菜单项。

模型如下:

'menu' = {
   'filters': [
      {
         'title': 'Unified Communications',
         'stub': 'unified communications',
         'turnedOn': false
      },
      {
         'title': 'News',
         'stub': 'news',
         'turnedOn': false
      },
      {
         'title': 'Events',
         'stub': 'events',
         'turnedOn': false
      }
   ],

   'categories': [
      {
         'title': 'News, Events & Updates',
         'categories': [
            {
               'title': 'Top Stories',
               'links': [
                  {
                     'title': 'News Archives',
                     'link': 'news/archives'
                  },
                  {
                     'title': 'News Subscriptions',
                     'link': 'news/subscriptions'
                  },
                  {
                     'title': 'News Submissions',
                     'link': 'news/submissions'
                  }
               ]
            },
            {
               'title': 'Upcoming Events',
               'links': [
                  {
                     'title': 'Past Events',
                     'link': 'events/past'
                  }
               ]
            }
         ],
         'links': [
            {
               'title': 'Competitive News',
               'link': '/news/competitive',
            },
            {
               'title': 'Leader Communications',
               'link': '/leader_communications',
            },
            {
               'title': 'Partner Regional News and Updates',
               'link': '/news/partner_regional',
            },
            {
               'title': 'Recent Updates',
               'link': '/news/recent',
            }
         ]
      }
   ] 
};

1 个答案:

答案 0 :(得分:5)

一个选项是在JavaScript中使用所有过滤器构建模型,并在每个过滤器上都有一个“turnOn”属性。然后将该属性绑定到每个复选框。

$scope.filterList = [
    {title: "title1", turnedOn: false}
]

<div ng-repeat="filter in filterList ">
    <label>{{filter.title}}</label>
    <input type="checkbox" ng-model="filter.turnedOn" />
</div>

然后使用filterList生成过滤器对象。