简体   繁体   中英

AngularJS directive DOM manipulation not executing link()

Trying to do some DOM manipulation, but link() is never called so nothing happens:

app.js

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

directives.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 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

 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
    }
  };
}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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