繁体   English   中英

角度指令不起作用

[英]angular directive not working

Angular指令不适用于以下情况。 在下面的角度应用程序中,我希望显示两种项目,它们存储在控制器中。 为了显示它们,我为这两种情况创建了指令,并使用ng-repeat遍历列表,但是未渲染项目。

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  <style media="screen">
    .selected {
      background: #cdcdcd;
    }
  </style>
</head>

<body ng-app="myApp">

<div ng-controller="ListController as listctrl">

  <div class="" ng-repeat="item in listctrl.items">
    <itemtype1 ng-if="item.type_ === 'type1'" val="item.val"></itemtype1>
    <itemtype2 ng-if="item.type_ === 'type2'" val="item.val"></itemtype2>
  </div>


</div>

<script type="text/javascript">
angular
  .module('myApp',[])
  .controller('ListController', function($scope) {
    var listctrl = this;
    listctrl.items = [];

    listctrl.items.push({'val':'A', 'type_': 'type1'});
    listctrl.items.push({'val':'B', 'type_': 'type2'});
    listctrl.items.push({'val':'C', 'type_': 'type1'});
    listctrl.items.push({'val':'D', 'type_': 'type2'});
  })
  .directive('itemtype1', function() {

    return {

      template: '<strong>{{$scope.val}}</strong>',
      restrict: 'E',
      scope: {
        val: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  })
  .directive('itemtype2', function() {

    return {

      template: '<i>{{$scope.val}}</i>',
      restrict: 'E',
      scope: {
        val: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  });
</script>

</body>
</html>

乍一看,我的第一个猜测是您应该更改以下内容:

template: '<strong>{{$scope.val}}</strong>'

对此:

template: '<strong>{{val}}</strong>'

暂无
暂无

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

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