【问题标题】:Using directives inside an ng-repeat在 ng-repeat 中使用指令
【发布时间】:2015-02-20 06:24:55
【问题描述】:

我正在尝试获得一个非常简单的 ng-repeat,它使用 repeat 中的指令,这些指令具有基于重复项的属性值。

我已经尝试过calling an angularjs attribute directive from inside an ng-repeat中的建议:

<tr ng-repeat="tagName in tagNames">
  <th>{{tagName}}</th>
  <td><div mass-autocomplete><input type="text" ng-model="tag.tagName" 
           mass-autocomplete-item="tag_options_fn" 
           dynamic-options="tagName" /> RE? 
       <input type="checkbox" ng-checked="re.tagName"/>  
       {{tagValuesMatchCount(tagName)}}</div></td>
</tr>

但这只是将文字“tagName”放在每一行中。

我尝试编写自己的指令(包括对 MassAutocomplete 的一些自定义更改),以便我可以编写:

<tr ng-repeat="tagName in tagNames">
  <td>{{tagName}}</td>
  <td><div tag-selection tagk="{{tagName}}"/></td>
</tr>

指令是:

.directive('tagSelection', function() {
    return {
        scope: {
            tagk: '@'
        },
        template: '<div mass-autocomplete><input type="text" \
             ng-model="tag.{{tagk}}" \
             mass-autocomplete-item="tag_options_fn" \
             dynamic-options="{{tagk}}"/> RE? \
             <input type="checkbox" ng-checked="re.{{tagk}}"/> \
             {{tagk}}</div>'
    }
})

但是 ng-model 部分在使用 {{tagk}} 时会出错(如果我使用文字就可以了)。最后我也没有得到方法绑定。

我很确定我可以使用一些巧妙的变量范围来完成这项工作,但我发现 doco 在这方面很混乱。

我可以在没有自定义指令的情况下使用纯 ng-repeat 执行此操作吗?或者我可以对自定义指令进行更改以使其正常工作吗?

我想要的输出(给定 tagNames = ["host","user","method"])是:

<tr>
    <th>host</th>
    <td><div mass-autocomplete><input type="text" ng-model="tag.host" 
          mass-autocomplete-item="tag_options_fn" 
          dynamic-options="host" /> RE? 
          <input type="checkbox" ng-checked="re.host"/> 
          {{tagValuesMatchCount("host")}}</div></td>
</tr>
<tr>
    <th>user</th>
    <td><div mass-autocomplete><input type="text" ng-model="tag.user" 
           mass-autocomplete-item="tag_options_fn" 
           dynamic-options="user" /> RE? 
           <input type="checkbox" ng-checked="re.user"/> 
           {{tagValuesMatchCount("user")}}</div></td>
</tr>
<tr>
    <th>method</th>
    <td><div mass-autocomplete><input type="text" ng-model="tag.method" 
           mass-autocomplete-item="tag_options_fn" 
           dynamic-options="method" /> RE? 
           <input type="checkbox" ng-checked="re.method"/> 
           {{tagValuesMatchCount("method")}}</div></td>
</tr>

Plunker 可用here

【问题讨论】:

  • 嘿,您不需要用mass-autocomplete 包装每个mass-autocomplete 项目。您可以在表格标签处设置mass-autocomplete

标签: javascript angularjs


【解决方案1】:

以下是正确的版本。

          <tr>
          <td colspan="2"><b>Actual output</b></td>
        </tr>
        <tr ng-repeat="tagName in tagNames">
            <td>{{tagName}}</td>
            <td><div tag-selection tagk="tagName"></div></td>
        </tr>

指令定义

   .directive('tagSelection', function() {
            return {

                template: '<div mass-autocomplete><input type="text" ng-model="tag[tagName]" mass-autocomplete-item="tag_options_fn" dynamic-options="{{tagName}}"/> RE? <input type="checkbox" ng-checked="re[tagName]"/> {{tag[tagName]}}</div>'
            }
        })

Here 是骗子http://plnkr.co/edit/QeFOHqM1oYtif47RKDFa?p=preview

【讨论】:

  • 这只是在我的结果源中产生了文字 tag[tagk],编译器没有将 tagk 转换为实际值(在本例中为 host/user/方法)。
  • 是否有任何理由在tagk: '@' 中使用@。如果tagk 是您父控制器范围内的变量,您可以使用tagk: '=' 来获得双向绑定。
  • 我从here 得到了@。现在将整理一个plunker。谢谢。
  • Plunker 添加到主要问题。
  • @SimonML 检查答案中更新的指令定义
猜你喜欢
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 1970-01-01
相关资源
最近更新 更多