该指令具有隔离范围,并且范围属性使用“@”传递。
这就是指令的调用方式:
<div ng-init="classForIcon = 'vladClass'"></div>
<div ng-init="textForIcon = 'Icon\'s text'"></div>
<div ng-init="routeForIcon = 'www.google.com'"></div>
<div ng-init="tooltipForIcon = 'my tooltip'"></div>
<div ng-init="imageForIcon = 'images/HOME_ICON1.png'"></div>
<rl-icon-widget icon-class="{{classForIcon}}" icon-text = "{{textForIcon}}" icon-tooltip="{{tooltipForIcon}}" icon-route="{{routeForIcon}}" icon-image="{{imageForIcon}}"></rl-icon-widget>
这是指令的定义方式:
'使用严格';
fcPortalApp.directive('rlIconWidget', ['localizationAssistant', function(localizationAssistant) {
var obj = {
restrict: 'E',
templateUrl: 'scripts/directives/rliconwidget/rliconwidget.html',
//require: 'ngModel',
scope: {
//ngModel: '@',
iconClass: "@",
iconRoute: "@",
iconText: "@",
iconTooltip: "@",
iconImage: "@"
},
link: function(scope, element, attrs) {
console.log(scope);
console.log(scope.iconImage);
console.log(scope.iconTooltip);
console.log(scope.iconRoute);
}
};
console.log(obj);
return obj;
}]);
当我检查范围对象时(单击console.log 的输出(firebug 中的范围_),看起来它的 iconImage、iconTooltip 和 iconRoute 属性设置正确。
然而 console.log(scope.iconImage)、console.log(scope.iconTooltip) 和 console.log(scope.iconRoute) 打印“未定义”