我一直在编写一个简单的角度指令,以在用户关闭浏览器时显示警报。
settings.py
未调用链接功能。我在这里想念什么
答案 0 :(得分:3)
您应该像这样调用指令
<are-you-sure></are-you-sure>
演示
var app = angular.module('myApp',[])
app.directive("areYouSure", ["$window", function ($window) {
return {
restrict: "E",
link: function() {
alert("I am called");
$window.onbeforeunload = function () {
alert("I am called");
return "are you sure?";
};
}
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<are-you-sure></are-you-sure>
</body>