首页 > 解决方案 > 如何更新 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工作原理,因为它在第一次加载时一切都很好。

那么应该使用什么来更新模板中的值呢?

标签: javascriptangularjs

解决方案


您可以使用$watch

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

这只是一个内联解决方案。但是,您应该使用scope.$watch().


推荐阅读