简单的标记:
<!DOCTYPE html>
<html ng-app="APP">
<head></head>
<body ng-controller="myController">
<script src="angular.min.js"></script>
<script src="controller.js"></script>
</body>
</html>
简单的代码示例:
angular.module('APP', []).controller('myController', function($scope) {
$scope.test = function() {
console.log('Weird behaviour!')
}
(function() {} ()); //if you comment self-executing function console will be empty
});
而且范围行为真的很奇怪。您能解释一下为什么会这样吗?
最佳答案
您无意中制作了 test
作用域方法 IIFE,当前代码本质上是
$scope.test = (function() {
console.log('Weird behaviour!')
})(undefined)
虽然 $scope.test
本身将是 undefined
。
应该是
$scope.test = function() {
console.log('Weird behaviour!')
};
(function() {} ());
分号很珍贵。
关于javascript - Angular 意外行为。自执行函数调用作用域函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33658561/