【发布时间】:2014-07-20 20:10:23
【问题描述】:
我是 AngularJS 的新手,并且有一个非常简单的示例无法正常工作。我希望有人能告诉我我错过了什么。它应该在鼠标输入时更改颜色并在单击时更改html:
http://plnkr.co/edit/fQ5nejywl1taPSpZuBVv?p=preview
index.html:
<!doctype html>
<html ng-app="app">
<head>
<script data-require="angular.js@1.3.0-beta.5" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<div my-dom-directive>Click me!!</div>
</body>
</html>
script.js
var app = angular.module('app', []);
(function(){
var directive = function() {
return {
link: function($scope, element, attrs) {
element.bind('click', function() {
element.html('Clicked I am!');
});
element.bind('mouseenter', function() {
element.css('background-color', 'yellow');
});
element.bind('mouseleave', function() {
element.css('background-color', 'white');
});
}
};
};
angular.module('app').directive('myDomDirective', directive);
})
如果我不使用自调用方法来添加指令,它可以正常工作。
谢谢。
【问题讨论】:
-
你错过了函数调用
标签: javascript angularjs angularjs-directive