简体   繁体   English

Angularjs ngInit重复3次-ngClick一次

[英]Angularjs ngInit repeats 3 times - ngClick once

I know there are a lot of other questions covering this topic, but none really answer my question. 我知道还有很多其他问题涉及这个主题,但是没有一个问题能真正回答我的问题。 I have a function that retrieves data from my db. 我有一个从数据库中检索数据的函数。 Now when i run the function on ngInit the function runs three times, but on an ngClick it only runs once, which is what I want. 现在,当我在ngInit上运行该函数时,该函数运行了3次,但是在ngClick它仅运行了一次,这就是我想要的。

I read in a couple of posts that it is normal for the function to repeat multiple times, because of the angular watchers that watches for the code to change. 我在几篇文章中读到,多次重复执行该功能是正常的,因为要angular watchers代码更改的angular watchers Now I really don't mind that the code is repeating, but the issue is that I set a variable to true when the function starts and to false when I get the data. 现在我真的不介意代码在重复,但是问题是我在函数启动时将变量设置为true ,而在获取数据时将变量设置为false But now my variable gets set to false , and the code runs again. 但是现在我的变量设置为false ,代码再次运行。 So my variable gets changed prematurely. 因此,我的变量过早更改。

Example: 例:

controller.getItem = function(){
    controller.loading = true;
    service.getSelectedItem($routeParams._id).then(function(response){
        if(response.data.state === 'success'){
            controller.myItem = response.data.item;
            controller.loading = false;
        }
    });

}

HTML: HTML:

<div id="item">
<!-- remove this comment to test ngInit -->
<!-- <div id="item" ng-init="controller.getItem()"> -->

    <button id="clickMe" ng-click="controller.getItem()">Get data</button>

    <div class="panel panel-new">

        <div class="panel-header">

            <div class="row">

                <div class="col-xs-12">
                    <h4>My Item - {{ controller.myItem._id }} 
                    <small>added on: {{ controller.myItem.dateAdded | date: 'dd MMM yyyy' }}</small></h4>
                </div>

            </div> <!-- Close div for .row -->

        </div> <!-- Close div for .panel-header -->

    </div>

</div>

Now my loading screen disappears and my content is still undefined . 现在,我的加载屏幕消失了,我的内容仍然undefined Only after running three times does the content actually get populated. 只有运行了三遍之后,内容才真正被填充。

As I said, when this function runs on an ngClick it works perfectly and only runs once, but on an ngInit it runs 3 times and does not work as intended. 就像我说的那样,当此功能在ngClick上运行时,它可以完美运行并且只能运行一次,但是在ngInit它可以运行3次,并且无法按预期运行。

I am not sure how to approach this issue. 我不确定如何解决此问题。

Can you please little bit more detail. 您能再请点细节吗? It should be better you could use nginit inside specific controller. 最好在特定控制器内使用nginit。 Once you calling particular controller then it will run perfectly according to behavior. 调用特定控制器后,它将根据行为完美运行。

Okay, so after a lot of debugging, I finally found the issue. 好的,经过大量调试,我终于找到了问题。 The problem was that I had this in my index page: 问题是我在索引页面中有这个:

<div ng-if="auth" id="ngView" class="ngView">
    <div ng-view>
    </div>
</div>

So what was I think was happening is that it created a view, ran the function, created a view, ran the function. 所以我认为正在发生的事情是它创建了一个视图,运行了该函数,创建了一个视图,然后运行了该函数。 So that is why everything was happening more than once. 这就是为什么一切都发生不止一次的原因。

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

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