简体   繁体   English

angularjs从控制器内部调用函数

[英]angularjs calling a function from inside a controller

pokeApp.controller('mycontroller', function($scope, $routeParams){


   // part 1: // why would this not work
   $scope.myFunc();     

   $scope.myFunc = function(){
       console.log("Hi !");
   }

   // part 2: // why would this not work

  this.helloWorld();
  this.helloWorld = function(){
       console.log("Hello World");
  }
}

Hi My question is why would these 2 things not work; 嗨,我的问题是,为什么这两种方法不起作用? i mean either are either in the controller or in the scope. 我的意思是要么在控制器中,要么在示波器中。 I know i can call a function simply defined 'function helloWorld(){...}' 我知道我可以调用一个简单定义的函数'function helloWorld(){...}'

thanks ! 谢谢 !

You're calling the function before it has been defined. 您在定义函数之前就在调用它。 Change your code to: 将您的代码更改为:

   $scope.myFunc = function(){
       console.log("Hi !");
   }
   $scope.myFunc();   

You expected function hoisting to happen: 您期望功能提升发生:

  myFunct(); function myFunct() { alert('hey'); } 

this would work. 这会工作。

But this wouldn't: 但这不会:

 myFunct(); var myFunct = function() { alert('hey'); } 

The similar case is going on with the controller scope property, which behaves exactly as a regular variable in this case, means no hoisting happens. 控制器作用域属性也发生了类似的情况,在这种情况下,它的行为与常规变量完全相同,这意味着不会发生起吊。

You'll find some great explanations about hoising here: var functionName = function() {} vs function functionName() {} . 您将在此处找到有关提升的一些很好的解释: var functionName = function(){}与function functionName(){}


So, to make everything in your original code work using the hoisting feature, it should look like this: 因此,要使用提升功能使原始代码中的所有内容正常工作,它应如下所示:

pokeApp.controller('mycontroller', function($scope, $routeParams){


   // part 1:
   myFunc();     

   function myFunc(){
       console.log("Hi !");
   }

   // part 2:

  helloWorld();
  function helloWorld(){
       console.log("Hello World");
  }
}

Or, a little hacky way to maintain scopes: 或者,一种有点笨拙的方式来维护范围:

pokeApp.controller('mycontroller', function($scope, $routeParams){


   // part 1:
   $scope.myFunc = myFunc; // this is the key, assigns a hoisted function value
                           // to the $scope object property which is then ready
   $scope.myFunc();     

   function myFunc(){
       console.log("Hi !");
   }

   // part 2:
  this.helloWorld = helloWorld;
  this.helloWorld();
  function helloWorld(){
       console.log("Hello World");
  }
}

Here's a snippet showing that in action: 这是一个片段,展示了它的作用:

  var myObj = {}; myObj.f = myFunct; myObj.f(); function myFunct() { alert('yay, it still works!'); } 

You can use hoisting to do this: 您可以使用提升来做到这一点:

app.controller('MainCtrl', function($scope) {

  $scope.myFunc = myFunc;
  $scope.myFunc();

  function myFunc(){
    console.log("Hi !");
  }

});

plunk unk

Heare is good article about it - http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html Heare是一篇很好的文章-http: //www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

PS Actually I can't see any reasons to do so in real practice... PS实际上,在实际实践中我看不出有任何理由这样做...

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

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