4

I have a controller that is referenced in some html. And the html changes on some event so the controller function code executes multiple times.
The problem is that I have a peace of code that needs to be executed only once.
Here is the controller:

angular.module("someModule", [dependencies])
.controller("leftBoardController", function ($scope, someService) {
   (function createFilter(dataService) {
    // here I'm loading all the data on which I want to operate after
            // this code should execute only once
    })(TempEmployeeService);
}

But it executes each time the html changes.
This is how the controller is inserted:

<section ng-controller="TreeMapController"
                 ng-class="{ 'main-container-single': zoomed }"
                 class="minContainer main-container">

Questions:
1) How to make the controller function execute only once?
2) Or is there a way I can write this peace of code that will execute only once?

4

1 回答 1

3

将您的“运行一次”代码放入服务中,并返回一个承诺。

将该服务注入您的控制器。

有关服务创建然后返回承诺以及控制器使用该承诺的示例,请参阅服务是否应该公开它们的异步性?

于 2013-07-23T18:12:44.980 回答