简体   繁体   English

控制器中定义的函数(javascript)与scope.function(angular函数)的区别

[英]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.

相关问题 角度$ scope无法正常工作。 试图在控制器$ scope和$ scope.function范围内分配对象值 - Angular $scope not working as expected. Trying to assign object values in a controller $scope and a $scope.function scope Angular中$ scope.Variable和$ scope.Function之间的差异? - Differnce between $scope.Variable and $scope.Function in Angular? 从$ scope.function()内刷新$ scope - Refreshing $scope from inside a $scope.function() AngularJS-在每个$ scope.function调用中重新定义$ scope中的数组 - AngularJS - array in $scope re-defined on each $scope.function call Karma测试对scope.function内部函数的调用 - Karma testing a call to a function inside a scope.function Angular指令:控制器的作用域与链接函数的作用域之间有什么区别? - Angular directive: whats the difference between scope in controller vs scope in link function? 通过右键单击从当前或新选项卡中的href调用Angular $ scope.Function? - Call an Angular $scope.Function from href in current or new tab by right click? JavaScript函数作用域差异 - Javascript function scope difference 你如何从href调用AngularJS $ scope.Function? - How do you call an AngularJS $scope.Function from href? javaScript中本地定义函数的范围 - Scope of locally defined function in javaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM