我有两个 div - 第一个包含第二个。包含的 div 有自己的 Controller 。当我单击容器中的图标按钮时,我会更改一个变量,该变量随后会影响所包含的 div 的可见性。
看起来像这样:
<div ng-controller="BarController">
<div class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="col-lg-2 page-title">My Page</div>
<div class="col-lg-10">
<span class="actions">
<i class="fa fa-lg fa-download fa-inverse" tooltip="Download"
ng-click="showSecondaryBar=!showSecondaryBar"></i>
</span>
</div>
</div>
</div>
<div class="download navbar download-in download-out"
ng-class="{'myhidden': !showSecondaryBar}"
ng-cloak>
<div class="col-lg-offset-4 col-lg-4 form-inline form-group" ng-controller="TagsController">
<div class="download-label col-lg-6">
<label>Download by tags:</label>
</div>
<div class="download-tags col-lg-6">
<tags-input class="bootstrap" spellcheck="false" min-length="1" ng-model="tags" add-from-autocomplete-only="true">
<auto-complete source="loadTags($query)" min-length="1" load-on-down-arrow="true"
load-on-focus="true" max-results-to-show="5"
highlight-matched-text="false"></auto-complete>
</tags-input>
</div>
</div>
</div>
</div>
<tags-input>
取自ng-tags-input我想在每次单击图标按钮时重置已经输入的标签(这会更改包含 ng-tags-input 的 div 的visilibyt)。
问题是,因为我有 TagsController
其中包含数据 ( tags
),并且此数据在 BarController
中不可见,我不知道如何重置 tags
数组变空。
我想过使用服务,但它的耦合性太强了。我希望在 TagsController
中有一个函数单击时调用。但我不知道如何从另一个 Controller 做到这一点
最佳答案
你是对的,你必须使用一项服务。 为什么不使用广播,因为您的 TagsController 包含在 BarController 中?
您可以在 BarController 中包含scope.broadcast("Event")
然后 TagsController 上的“开启”监听器将在“事件”发生时重置标签数组。
我个人愿意这样做。
https://docs.angularjs.org/api/ng/type/ $rootScope.Scope
关于javascript - 单击后重置不同 Controller 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30213269/