ng-repeat和多态指令

时间:2017-05-12 09:53:54

标签: angularjs angularjs-directive

我想知道是否有办法以这种方式构建一个自定义Element指令以与ng-repeat一起使用:

<div class="list">
      <my-custom-directive class="item item-icon-right" ng-repeat="a in concepts">
          <p>{{::a.label}}</p>
          <i class="icon ion-forward muted"></i>
      </my-custom-directive>
</div>

my-custom-directive如果a.href存在,则应在锚点中进行编译,如果不存在则> 。

问题基本上只是设计:我有一些没有href的项目,但它们仍然应该在列表中。在Ionic1中,看起来我可以执行div列表或a列表,但不会在不破坏列表设计的情况下进行混合..

1 个答案:

答案 0 :(得分:1)

当然可以。像这样:

<my-custom-dir ng-repeat="a in concepts"></my-custom-dir>

其中,指令就像,

app.directive('myCustomDir', function() {
  return {
    restrict: 'E',
    templateUrl: 'custom.html'
  }
})

并且custom.html模板,

<p ng-hide="a.href">{{::a.label}}</p>
<a ng-href="{{a.href}}" ng-show="a.href">{{::a.label}}</a>
<i class="icon ion-forward muted"></i>

另外,我保持$scope.concepts有虚拟对象,如下所示,

$scope.concepts = [{
    href: 'eample.com',
    label: 'example'
  }, {
    label: 'example1'
  }, {
    href: 'eample2.com',
    label: 'example2'
  }]

working example

尽管如此,我认为您div.item内应该ng-repeat .list div.item。在$(document).ready(function () { $('button.open-modal').on('click', function (e) { // Make sure the click of the button doesn't perform any action e.preventDefault(); // Get the modal by ID var modal = $('#myModal2'); // Set the value of the input fields modal.find('#edit-name').val($(this).data('task')); modal.find('#edit-description').val($(this).data('description')); modal.find('#edit-status').val($(this).data('done')); // Update the action of the form modal.find('form').attr('action', 'tasks/update/' + $(this).val()); }); }); 你应该能够拥有你想要的任何东西(不确定ionic1如何处理它)

相关问题