简体   繁体   English

AngularJS指令DOM操作未执行link()

[英]AngularJS directive DOM manipulation not executing link()

Trying to do some DOM manipulation, but link() is never called so nothing happens: 尝试执行一些DOM操作,但是从未调用link()所以什么也没发生:

app.js app.js

var app = angular.module('app', ['directives', ...]);
var directives = angular.module('directives', []);
...

directives.js 指令.js

directives.directive('doIt', ['$window', function($window) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            console.log('Inside link()');
            // Do stuff with $window
        }
    };
]});

HTML 的HTML

<html ng-app="app">
    <body>
        <div ng-view>
            <div do-it>
                // ....
            </div>
        </div>
    </body>
</html>

What am I missing? 我想念什么?

Your square bracket is incorrect 您的方括号不正确

directives.directive('doIt', ['$window', function($window) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            console.log('Inside link()');
            // Do stuff with $window
        }
    };

    }]);//Check this part. The square bracket was on the wrong side of }

Move it like like I have it above 像我上面一样移动它

You also have some extra periods in your module decleration 您的模块清算还需要一些额外的时间

var app = angular.module('app', ['directives']); //remove the extra ....'s //删除多余的...

is this plnkr help? 这是plnkr的帮助吗? plnkr nk

 angular.module('app', [])
// .controller('Controller', ['$scope', function($scope) {
//   $scope.customer = {
//     name: 'Naomi',
//     address: '1600 Amphitheatre'
//   };
// }])
.directive('doIt', ['$window', function($window) {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
        console.log('Inside link()');
        // Do stuff with $window
    }
  };
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM