1

这是不言自明的代码,请查看 html 部分中的注释:

HTML:

<div ng-app='myApp'>
<div ng-controller="MCtrl">
  <!-- this should be "test:", OK -->
  <test></test>
  <br>
  <!-- this should be "test: Hello world!", OK -->
  <test custom-target="helloModel"></test>
  <br>
  <!-- this should be "test: Hello !", FAIL! -->
  <test custom-target="emptyModel"></test>
  <br>
</diV>
</div>

JS:

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

function MCtrl($scope) {
    $scope.helloModel = 'world';
    $scope.emptyModel = '';
}

myApp.directive('test', function() {
   return {
      restrict: 'E',
      scope: {
         customTarget: '='
      },
       template: '<span>test: <b ng-show="customTarget">Hello, {{customTarget}}!</b></span>'
   };
});

http://jsfiddle.net/kMybm/34/

简而言之,我需要属性custom-target是可选的,并且能够检测到它何时丢失。

升级版:

目前找到了这个解决方案:

http://jsfiddle.net/kMybm/35/

也许有更好的。

4

1 回答 1

1

b第三个示例中的标记根本不显示,因为您在其上安装了 ngShow,当 customTarget 为空字符串时,它的计算结果为 false。

于 2013-07-08T12:24:34.870 回答