我无法弄清楚隔离范围是如何工作的。当我注释指令的范围部分时,我的代码似乎工作正常。谁能解释一下我错过了什么?
export function ExampleDirective() {
'ngInject';
let directive = {
restrict: 'E',
templateUrl: 'someurl.html',
scope: {
theFilter: '=' //isolated - if i make the scope global, it works
},
controller: ExampleController,
controllerAs: 'vm',
bindToController: true,
link: linkFunc
};
function linkFunc(scope, el, attr, vm) {
}
return directive;
}
class SymptomSearchController {
constructor ($scope) {
'ngInject';
}
}
<小时/>
<input type="text"
class="form-control"
ng-model="theFilter">
<小时/>
<example-directive theFilter="main.theFilter"></example-directive>
<小时/>
export class MainController { //as 'main'
constructor() {
'ngInject';
this.theFilter = "test";
}
}
最佳答案
由于您的 Controller 内部指令有别名,因此您应该在访问 Controller 的变量之前使用它。在 ng-model
值中添加 vm.
。
<input type="text"
class="form-control"
ng-model="vm.theFilter">
关于javascript - 两种方式隔离绑定(bind) Angular Directive(指令) es6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36016498/