Angular - 指令链接未被调用

时间:2014-01-08 20:40:52

标签: angularjs angularjs-directive

我写了一个指令,它有一个没有被调用的链接函数,模板被注入到DOM

function() {
'use strict';

// Define the directive on the module.
// Inject the dependencies. 
// Point to the directive definition function.
angular.module('app').directive('nvVideo', ['$window', nvVideo]);

function nvVideo ($window) {
    // Usage:
    // 
    // Creates:
    // 
    var directive = {
        link: function(scope, element, attrs) {
        alert('ggg');
        },
        restrict: 'EA',
        template: '<div id="slot1">video slot</div>'


    };
    return directive;

}
})();

1 个答案:

答案 0 :(得分:0)

<强> HTML

<div ng-app='app'>
    <n-video></n-video>
</div>

<强>指令

angular.module('app', [])
.directive('nVideo', ['$compile', function ($compile) {

    return{
        restrict: 'EA',
        template: '<div id="slot1">video slot</div>',
        link: function(scope, element, attrs) {
            alert('ggg');
        }
    }
}]);

<强> JSFIDDLE