0

使用角度指令时,我看到了一种奇怪的行为。

在下面的代码中

HTML

<body ng-app="loadTweetsModule">
    <div tweets> Load </div>
    <div loadTweets> loadTweets </div>
</body>

Javascript

var loadTweetsModule = angular.module("loadTweetsModule",[]);


loadTweetsModule.directive('tweets',function(){
    return {
        link : function(scope,element){
            element.bind("mouseenter", function(){
                console.log("tweets");
            });
        }
    };
});

loadTweetsModule.directive('loadTweets',function(){
    return {
        link : function(scope,element){
            element.bind("mouseenter", function(){
                console.log("loadTweets");
            });
        }
    };
});

JSFiddle 链接

除了名称之外,这两个指令loadTweetstweets是相同的。该指令tweets按预期工作,但loadTweets不起作用。我无法找出这种行为的原因。有人可以解释一下吗?

4

1 回答 1

2

如果你用js写,loadTweets你应该用html写load-tweets

来自http://docs.angularjs.org/guide/directive

Directives have camel cased names such as ngBind. The directive can be invoked by translating the camel case name into snake case with these special characters :, -, or _. Optionally the directive can be prefixed with x-, or data- to make it HTML validator compliant. Here is a list of some of the possible directive names: ng:bind, ng-bind, ng_bind, x-ng-bind and data-ng-bind.

于 2013-08-29T13:30:25.823 回答