javascript - 单击后重置不同 Controller 的数据

标签 javascript jquery angularjs ng-tags-input

我有两个 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/

相关文章:

javascript - 幻灯片 : adding a div for box-shadow effects has jQuery selecting the div 的 jQuery 帮助

javascript - 将 javascript 数组发送到 django View

javascript - 在 AngularJS 中重置表单

javascript - array.forEach 和 angular.forEach 哪个更高效?

javascript - 如何将值从 <a> ( anchor 标记)传递到 React js 中的函数?

javascript - 如何获得主干分页以返回正确的长度?

javascript - 如何在 JavaScript 中操作 DOM 中的特定字符?

使用 BundleTransformer.UglifyJs 混淆 Javascript

javascript - block 在 Masonry 中与 jQuery 不对齐

javascript - AngularJs 指令将用户输入作为字符串并将其重新格式化以交替大小写。例如,计算机变成了计算机