【发布时间】:2015-04-07 21:11:18
【问题描述】:
我的 ng-repeat 中有以下自定义指令:
<action ng-click="addToCart('event', d, type.id, $index )" text="Hey!" state="ready"></action>
action.js:
(function() {
angular.module('vendor').directive('action', function() {
return {
restrict: 'E',
scope: {
text: '@text',
state: '@state'
},
templateUrl: '/js/app/templates/action.html',
link: ['$http', '$scope', '$element', '$attrs', function($http, $scope, $element, $attrs) {
$scope.text = $attrs.text;
}],
controller: ['$http', '$scope', '$element', '$attrs', function($http, $scope, $element, $attrs) {
$scope.text = $attrs.text;
}],
controllerAs: 'action'
}
});
}) ();
action.html:
<a ng-class="{ 'btn-set': isReady || isWorking || isComplete || hasFailed, 'btn-ready-state': isReady, 'btn-working-state': isWorking && selectedIndex == $index, 'btn-complete-state': isComplete && selectedIndex == $index, 'btn-failed-state': hasFailed }">
<i ng-if="isReady" class="fa fa-shopping-cart"></i>
<img ng-src="/images/loading.GIF" class="img" ng-show="isWorking && selectedIndex == $index "></span>
<i ng-if="isComplete && selectedIndex == $index " class="fa fa-check"></i>
<span class="text">
<span ng-if="isReady"><% text %></span>
</span>
</a>
我有一个自定义指令 action 带有自定义属性 text 和 state,文本属性就是指令内显示的文本。但是我的代码似乎不起作用(我没有错误),我是 angularjs 的新手,所以我想我在某处犯了一些新手错误。
注意:我将 {{ }} 更改为 所以它不会与我的 laravel 刀片模板冲突
【问题讨论】:
-
Chrome 开发工具/Firebug 的控制台或网络选项卡中没有错误?
-
没有显示错误。
-
您在元素选项卡中看到了什么?我猜指令内容在那里,但没有显示为您的
ng-show和ng-if表达式中的变量在指令的范围内不存在
标签: angularjs angularjs-directive