[英]Difference of function(javascript) vs scope.function(angular function) defined in controller
I want to know the difference between declaration of this two function in angular controller 我想知道角度控制器中这两个函数的声明之间的区别
function demo() {
};
scope.demo = function() {
};
Whether this two function are similar in performance or not?,Which one is the better option? 这两个功能在性能上是否相似?,哪一个更好?
I know only one difference that watch can be applied to a function which is in the scope means angular directive or element can't call javascript function. 我知道只有一个区别是watch可以应用于范围内的函数意味着angular指令或元素不能调用javascript函数。
Consider the following controller: 考虑以下控制器:
app.controller('Home', function($scope){
function thisIsAPrivateMethod(){
alert('Hello world');
}
$scope.thisIsAPublicScopedMethod(){
alert("I'm shown!");
}
thisIsAPrivateMethod(); // will trigger the alert everytime HomeController is instansiated
});
in the view: 在视图中:
<div ng-controller="Home">
<button ng-click="thisIsAPrivateMethod()">I will not work</button>
<button ng-click="thisIsAPublicScopedMethod()">I should display an alert</button>
</div>
As you see, the private method is only accessible within the controller code itself. 如您所见,私有方法只能在控制器代码本身中访问。
I think the only difference is the visibility of the function. 我认为唯一的区别是功能的可见性。 The first one will be global, and the second one can only referred through a angular
scope
variable. 第一个是全局的,第二个只能通过角度
scope
变量引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.