尝试做一些DOM操作,但永远不会调用link()
,所以没有任何反应:
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>
我错过了什么?
答案 0 :(得分:1)
方括号不正确
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']);
//删除额外....的
答案 1 :(得分:1)
这是plnkr的帮助吗?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
}
};
}]);