简体   繁体   English

AngularJS控制器内部的功能

[英]Function inside the AngularJS Controller

I have code snippet in which there is a Angular Modular Controller, but there is a function inside the same controller and with a call, which is raising doubt in my mind that is this way of coding allowed in Javascript or Angular? 我有一个代码片段,其中有一个Angular Modular Controller,但是在同一个控制器内部有一个函数并且有一个调用,这在我的脑海中引起了怀疑,这是Javascript或Angular中允许的这种编码方式吗? If Yes then how does it read it? 如果是,那么它是如何阅读的? See the below code format I have: 请参阅以下代码格式:

obj.controller('CartController',function($scope){

  $scope.totalCart = function(){
    var total = 10;     
    return total;
  }
  function calculate(){
    ...Some Logic..
  }

  $scope.$watch($scope.totalCart, calculate);
)};

Please help me to understand that is this type of function definition and call within a controller allowed in Angular/Javascript? 请帮助我理解这种类型的函数定义和在Angular / Javascript中允许的控制器内调用?

The calculate() is a private function -- it is only accessible in the scope of CartController . calculate()是一个私有函数 - 它只能在CartController的范围内CartController If you do not need to make use of your function in the view it is good idea to make it private. 如果您不需要在视图中使用您的功能,最好将其设为私有。 It says that it is not intended to be used in the view, so if someone else will be working with this code should think twice before use it in the view. 它表示它打算在视图中使用,因此如果其他人将使用此代码,请在视图中使用它之前再三思。 Moreover: from within calculate you have access to all objects that are accessible in the scope of the CartController (including objects passed to CartController as parameters). 而且:从calculate你可以访问CartController范围内可访问的所有对象(包括作为参数传递给CartController对象)。

Function declared in this way is a proper JS function , which means you can get reference to it by its name. 以这种方式声明的函数是一个正确的JS function ,这意味着您可以通过其名称来引用它。 Sometimes it is thought to be more readable if you declare / create your function in advance and only then assign them to properties of some other object (in this case $scope ): 有时,如果您事先声明/创建函数并将它们分配给其他对象的属性(在本例中$scope ),则认为它更具可读性:

function someFn (...) { ... }

function someOtherFn (...) { ... }

...

$scope.someFn = someFn

In the above snippet the intentions are very clear: make someFn accessible, while keep someOtherFn private. 在上面的片段中,意图非常清楚:使someFn可访问,同时保持someOtherFn私有。

Btw. 顺便说一句。 declaring functions like: function nameFn(...){...} is called function statement ; 声明函数如: function nameFn(...){...}被称为函数语句 ; you can very similarly do it like: var nameFn = function(...) {...} (so called function expression ). 你可以这样做: var nameFn = function(...) {...} (所谓的函数表达式 )。 There is a slight difference between those -- basically it is illegal: 这些之间略有不同 - 基本上它是非法的:

 someFn();
 var someFn = function(...) {...}

whereas, this works: 然而,这有效:

 someFn();
 function someFn(...) {...}

Sometimes you are forced to use this pattern, look eg at my answer to this question . 有时候,你不得不使用这种模式,我看如回答这个问题

$scope.launch = function (which) {

};
var _func = function () {...}

允许定义,它具有相同的影响

$scope.$watch($scope.totalCart, function(){..some logic...})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM