繁体   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