在角1.5同级部件和/或指令之间进行通信的最佳方法是什么。我用的是角质材料。我在使用ui路由器。
我试图保持组件和指令的分离,而不是相互依赖。
我想在适当的情况下将我的指令重构为.component()模块。
我有一个导航条,它被分割成一个指令( navbar )。在那个导航栏中,我有一个搜索栏,我想过滤一个列表。列表在同级指令中。
最初,我在ui-路由器定义的MainCtrl范围之外使用了navbar指令(并试图将其用作MainCtrl())。这对我来说似乎是有意义的,因为在整个应用程序中,导航栏相对一致。
我推迟将其放入MainCtrl的作用域中,然后可以将属性从MainCtrl绑定到navBar指令中的元素。这似乎是错误的,因为现在navBar和fileBrowser与MainCtrl耦合在一起。
我正在研究的其他选择:
使用和作用域.$watch()从子组件navBar中定义父组件的属性。然后,在另一个子组件fileBrowser中,使用scope.$ watch ()监视父组件中的这些更改,并相应地做出响应。
使用服务存储数据和传递数据。
使用$emit,$broadcast事件。
在这种情况下,保持指令/组件分离的最佳解决方案是什么?在同级指令/组件之间进行通信的最佳/最干净/推荐的方法是什么?
此状态由ui-路由器启动。
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.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>发布于 2016-10-24 17:32:43
要进行通信,亲缘关系组件使用双向绑定:
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>https://stackoverflow.com/questions/40222794
复制相似问题