角度1.5兄弟组件和/或指令之间进行通信的最佳方式是什么。 我正在使用棱角分明的材料 我正在使用ui-router。
我试图将我的组件和指令分开,而不是彼此依赖。
我想在适当的情况下将我的指令重构为.component()模块。
我有一个导航栏,我将其分成指令(navBar)。在那个导航栏中,我有一个搜索栏,我想过滤一个列表。该列表是兄弟指令。
最初,我在ui-router定义的MainCtrl范围之外使用了navbar指令(并尝试将其用作.compontent())。这似乎对我有意义,因为导航栏在整个应用程序中会相对一致。
我推迟将它放在MainCtrl的范围内,然后我可以将MainCtrl中的属性绑定到我的navBar指令中的元素。这似乎是错误的,因为现在navBar和fileBrowser与MainCtrl结合。
我正在研究的其他选项:
使用和范围。$ watch()从子组件navBar定义父组件的属性。然后在另一个子组件fileBrowser中,使用范围。$ watch()来监视父组件中的这些更改并做出相应的响应。
使用服务存储数据并传递数据。
使用$ emit,$ broadcast events。
在这种情况下,保持指令/组件分离的最佳解决方案是什么?在兄弟指令/组件之间进行通信的最佳/最干净/推荐方式是什么?
此状态由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>
我希望navbar过滤位于兄弟指令或组件文件浏览器
中的列表navbar.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;
}
navbar.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>
这是我要过滤的列表所在的位置。
filebrowser.js
angular.module('glossa')
.directive('fileBrowser', fileBrowser);
function fileBrowser() {
var directive = {
restrict: 'E',
templateUrl: 'components/filebrowser/filebrowser.html'
};
return directive;
}
filebrowser.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>
答案 0 :(得分:1)
要在兄弟组件之间进行通信,请使用双向绑定:
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>