为什么这个 angularjs 指令不起作用

Why does this angularjs directive not work?

本文关键字:指令 不起作用 angularjs 为什么      更新时间:2023-09-26

我试图遵循有关AngularJS指令的教程。 我一直无法在代码中找到任何语法错误,我什至尝试添加jQuery。

main.js

var app = angular.module("app", []);
app.directive = ("enter", function () {
    return function (scope, element) {
        element.bind("mouseenter", function () {
            console.log("I'm inside of you!");
        });
    }
});

ng.php

<body ng-app="app">
<div enter>I'm content!</div>
</body>

AngularJS和main.js都正确链接。

未报告控制台错误。

我错过了什么?

app.directive是一个函数,而不是一个属性。

您在哪里:

app.directive = ('enter'...

应该是:

app.directive('enter', function() ...)
因此,您

调用函数来注册指令,而不是使用赋值重写,这样您就再也不会有任何指令了。 :)