运行一次AngularJS控制器负载时负载、控制器、AngularJS

2023-09-13 03:40:13 作者:人不奋斗,何来享受

我有一些东西,仅需要在控制器被加载一次。什么是最好的方法?我读过一些关于跑块,但我真的不明白它是如何工作的。

I have some stuff that needs to be done only once when the controller is loaded. What's the best way to this? I've read some about the "run block" but I don't really understand how it works.

一些伪code:

when /app
resolove some stuff
load a view
controllerA

ControllerA:

Some-magic-piece-of-code-only-run-once{
 }

任何人都可以点我在正确的方向?

Can anyone point me in the right direction?

推荐答案

只要做到这一点,通常,控制器只需用$范围,服务功能的注入。当控制器被加载,这个函数被调用仅曾经。你可以做任何事情里面。

Just do it normally, controllers are just functions injected with $scope, services. When the controller is loaded, this function is called only once. You can do anything inside it.

app.controller("yourController",function($scope, myService){
   //Write your logic here.


   //Initialize your scope as normal
});