簡體   English   中英

如何更新 ng-template 中的值

[英]How to update values in ng-template

我有這個模板

<script type="text/ng-template" id="template">
    <span class="count-container">
        <span >{{count}}</span>
    </span>
</script>

我用不同的ng-init多次包含這個

<span ng-include="'template'" ng-init="count=$ctrl.getTotalCount()"></span>

<span ng-include="'template'" ng-init="count=$ctrl.tabs[0].errorCount"></span>

這在第一次加載時工作正常,但是當組件中的值更新時,這些值不會反映在加載模板的位置。 我想這是由於ng-init工作方式,因為它在第一次加載時一切正常。

那么應該使用什么來更新模板中的值呢?

您可以使用$watch

ng-init="$watch('count=$ctrl.tabs[0].errorCount')"

這只是一個內聯解決方案。 但是,您應該使用scope.$watch()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM