簡體   English   中英

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

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

嘗試執行一些DOM操作,但是從未調用link()所以什么也沒發生:

app.js

var app = angular.module('app', ['directives', ...]);
var directives = angular.module('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>

我想念什么?

您的方括號不正確

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 }

像我上面一樣移動它

您的模塊清算還需要一些額外的時間

var app = angular.module('app', ['directives']); //刪除多余的...

這是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