javascript - Angular 1 自定义指令未执行

标签 javascript angularjs angular-directive

我的指令定义如下:

'use strict;'

angular.module('clientApp')

.directive('afterLast', ['$ocLazyLoad','$timeout',function($ocLazyLoad, $timeout){
    console.log('entered directive');
    return {
        restrict: 'A',
        link: function(scope, elem, attr){
            if (scope.$last === true){
                console.log('entered directive')
                $timeout(function(){
                    $ocLazyLoad.load(['some files'])   
                })
            }
        }
    }
}]);

并且,我将其用作属性,如下所示:

<div ng-repeat="topicObject in module.topics track by $index" afterLast>

  <div class="ft-section">

    <div ng-repeat="learningPointObject in topicObject.learningPoints track by $index">
        <div class="ft-page">
            <h2 class="module-name" style="text-align: center;">{{module.name | capitalize:true}}</h2>
            <h3 class="topic-name">{{topicObject.name | capitalize:true}}</h3>
            <h4>{{learningPointObject.name | capitalize}}</h4>
            <hr>
         </div>
     </div>

  </div>

</div>  

但是我的指令没有执行。即使链接函数内部和外部的 console.log 语句也不起作用。 1. 我是否以正确的方式使用指令? 2. 如果是,可能是什么原因导致它不起作用?

最佳答案

在 HTML 中,指令名称需要采用短横线大小写,而不是驼峰大小写。

 <!-- ERRONEOUS camelCase
 <div ng-repeat="topicObject in module.topics track by $index" afterLast>
 -->

 <!-- USE kebab-case -->
 <div ng-repeat="topicObject in module.topics track by $index" after-last>

有关详细信息,请参阅 AngularJS Developer Guide - Directive Normalization

关于javascript - Angular 1 自定义指令未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42336374/

相关文章:

javascript - 使用 babel 和 webpack 减少转译代码助手

javascript - 由于异步执行 ble.scan,基于设备名称的 Cordova BLE 自动连接(无需用户操作)失败

javascript - 如何在 JavaScript 中同步 API 响应?

angular - 如何以编程方式实例化和应用指​​令?

javascript - 如何在现有父指令元素和子指令元素之间动态嵌套指令元素?

javascript - 如何使用下拉选择填充文本字段

javascript - 什么可能导致 Angular 无法正常渲染?

javascript - 类型错误 : Cannot read property '0' of undefined in Angularjs

javascript - 如何从自定义指令访问元素属性,同时保持对父范围的访问?

javascript - Angular Directive(指令)的路由问题