0

在 Angular 1.5 兄弟组件和/或指令之间进行通信的最佳方式是什么。我正在使用角材料。我正在使用ui路由器。

我试图让我的组件和指令分开,而不是相互依赖。

我想在适当的情况下将我的指令重构为 .component() 模块。

我有一个导航栏,我已将其分成一个指令(导航栏)。在那个导航栏中,我有一个搜索栏,我想过滤一个列表。该列表位于同级指令中。

最初,我在 ui-router 定义的 MainCtrl 范围之外有 navbar 指令(并试图将其用作 .compontent())。这对我来说似乎很有意义,因为导航栏在整个应用程序中都相对一致。

我推迟将它放在 MainCtrl 的范围内,然后我可以将 MainCtrl 中的属性绑定到我的 navBar 指令中的元素。这似乎是错误的,因为现在 navBar 和 fileBrowser 与 MainCtrl 相结合。

我正在研究的其他选项:

使用和 scope.$watch() 从子组件 navBar 定义父组件的属性。然后在另一个子组件 fileBrowser 中,使用 scope.$watch() 来监视父组件中的这些变化并做出相应的响应。

使用服务来存储数据和传递数据。

使用 $emit, $broadcast 事件。

在这种情况下,将我的指令/组件分开的最佳解决方案是什么?在兄弟指令/组件之间进行通信的最佳/最干净/推荐的方式是什么?

此状态由 ui-router 发起

main.component.js

angular.module('glossa')
    .component('mainComponent', {
        controller: MainCtrl,
        controllerAs: 'vm',
        transclude: true,
        templateUrl: 'app/main/main.html'
    });

function MainCtrl($scope, nodeSrvc, fileSrvc) {
    var vm = this;

    vm.selectedFile = {};
    vm.fileList = [];
    vm.searchText = '';
    vm.filteredFiles = [];

    activate();

    function activate() {
        buildFileList();
    }

    /**
     * Queries for all files in db.
     */
    function buildFileList() {
        fileSrvc.queryAllFiles().then(function(docs) {
            vm.fileList = docs;
        });
    }
}

main.html

//where the input where I filter the list
<navbar-directive></navbar-directive>

<div flex layout="row" >
    //where the list is located
    <file-browser layout="column"></file-browser>
    <tabbar></tabbar>
</div>

<drawer-directive></drawer-directive>

我希望导航栏过滤位于兄弟指令或组件文件浏览器中的列表

导航栏.directive.js

angular.module('glossa')
    .directive('navbarDirective', navBarDirective);

function navBarDirective(fileSrvc) {
    var directive = {
        restrict: 'E',
        replace: true,
        controller: NavbarCtrl,
        controllerAs: 'navVm',
        templateUrl: 'components/navbar/navbar.html',
        bindToController: true
    };
    return directive;
}

导航栏.html

<md-toolbar
        layout="row"
        class="nav-content primary-bg"
        md-whiteframe="1"
        md-selected-nav-item="currentNavItem"
        nav-bar-aria-label="navigation links">
    <span flex></span>
    <div class="md-toolbar-tools">
        <md-input-container md-no-float flex >
            <form ng-submit="vm.searchSubmit()">
                <input ng-model="vm.searchText" placeholder="Search...">
            </form>
        </md-input-container>
    </div>
</md-toolbar>

这是我要过滤的列表所在的位置。

文件浏览器.js

angular.module('glossa')
    .directive('fileBrowser', fileBrowser);

function fileBrowser() {
    var directive = {
        restrict: 'E',
        templateUrl: 'components/filebrowser/filebrowser.html'
    };
    return directive;
}

文件浏览器.html

<md-sidenav
        md-component-id="left"
        md-is-locked-open="true"
        layout="column">
    <md-content>
        <md-list flex>
            <md-list-item ng-repeat="file in vm.filteredFiles = (vm.fileList | filter: vm.searchText)" class="md-2-line">
                <md-item-content md-ink-ripple layout="row" layout-align="start center">
                    <div class="md-list-item-text" layout="column">
                        <h3>{{file.name}}</h3>
                        <p>Preview of first few lines of a baseline</p>
                    </div>
                </md-item-content>
            </md-list-item>
        </md-list>
    </md-content>
</md-sidenav>
4

1 回答 1

1

要通信 berween 兄弟组件,请使用双向绑定:

angular.module('glossa')
    .directive('navbarDirective', navBarDirective);

function navBarDirective(fileSrvc) {
    var directive = {
        //Use bi-directional binding
        scope: { 
            searchText: '='
        },
        restrict: 'E',
        replace: true,
        controller: NavbarCtrl,
        controllerAs: 'navVm',
        templateUrl: 'components/navbar/navbar.html',
        bindToController: true
    };
    return directive;
}

HTML

<nav-bar-directive search-text="main.searchText">
</nav-bar-directive>

<sibling-component search-text="main.searchText">
</sibling-component>
于 2016-10-24T17:32:43.103 回答