1

我很難弄清楚隔離範圍是如何工作的。我的代碼似乎工作正常,當我評論指示的範圍部分。任何人都可以解釋我錯過了什麼嗎?雙向隔離綁定角度指令es6

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"; 
    } 
} 

回答

1

像處理內部指令控制器的別名,所以你應該訪問控制器的變量之前使用它。在ng-model內添加vm.值。

<input type="text" 
     class="form-control" 
     ng-model="vm.theFilter">