【问题标题】:Custom Directive Not Showing自定义指令未显示
【发布时间】: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 带有自定义属性 textstate,文本属性就是指令内显示的文本。但是我的代码似乎不起作用(我没有错误),我是 angularjs 的新手,所以我想我在某处犯了一些新手错误。

注意:我将 {{ }} 更改为 所以它不会与我的 laravel 刀片模板冲突

【问题讨论】:

  • Chrome 开发工具/Firebug 的控制台或网络选项卡中没有错误?
  • 没有显示错误。
  • 您在元素选项卡中看到了什么?我猜指令内容在那里,但没有显示为您的 ng-showng-if 表达式中的变量在指令的范围内不存在

标签: angularjs angularjs-directive


【解决方案1】:

将您的字符串更改为包含在'' 中,并在您的action.html 中使用{{}}

<action ng-click="addToCart('event', d, type.id, $index )" text="'Hey!'" state="'ready'"></action>

在您的 HTML 中:

<span ng-if="isReady">{{ text }}</span>

顺便说一句,当你的作用域变量与属性同名时,你不需要提及它,你可以这样做:

scope: {
     text: '@',
     state: '@'
 },

编辑: 正如 squiroid 在他的问题中指出的那样,我认为您的链接/控制器功能有点偏离。首先,您不需要使用[,因为它们是用来修复注入的。你可以这样做:

link: function(scope, element, attrs) {
   //now you have scope.text, you don't need to assign from attrs
},

$http 服务应该被注入指令本身,而不是链接/控制器函数,像这样:

.directive('action', ['$http', function($http) {
   ...
}]

此外,无需覆盖链接/控制器中的文本。您只需要使用scope.text,因为它来自您的隔离范围声明。

【讨论】:

  • 好的,您的帖子对我有所帮助,但我在这里遇到了问题,$scope.isReady 正在由另一个控制器/服务设置,但我的指令似乎没有注意到这一点。
  • @user3718908 根据您的 HTML,isReady 是范围内的变量,但您尚未声明它。你需要把它连接到什么东西上。你是怎么设置的?
  • Is ready 在另一个控制器中声明,该控制器也包含在同一页面上,它的值可以是 true 或 false。
  • @user3718908 您为指令使用了隔离范围。它只知道text & state,没有其他参数。所以它在任何时候都是未定义的。
  • 只需将isReady: '&amp;'、isWorking: '&'、` 等添加到scope 选项中。试试看in a Plunker
【解决方案2】:

据我所知,除了两件事之外,一切都很好

<span ng-if="isReady">{{ text }}</span>

这里的第二个指令链接函数顺序首先是范围,第二是元素,第三个属性,最后是父控制器。如果你想在链接中使用$http,你必须将它注入指令本身而不是链接中不是由 DI 填充的。

link: function($scope, $element, $attrs) {
                $scope.text = $attrs.text;
            },

希望对你有帮助:)

【讨论】:

  • 好的,虽然 $scope.isReady 是从另一个控制器/服务设置的,但我的指令似乎没有注意到这一点,有什么原因吗?
  • 这里 $scope 是别名,您可以将其命名为 abc 等,如果它没有隔离范围,则由指令的父级填充。
  • 好的,现在我真正想知道的是如何从我的指令中访问 isReady。
【解决方案3】:

尝试添加

transclude: true,

和 范围: { 文本:'=', 状态:'=' }

那么你的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多