我在弄清楚隔离范围是如何工作时遇到了麻烦。当我注释指令的作用域部分时,我的代码似乎运行良好。谁能解释我所缺少的吗?

  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内部指令中具有别名,因此在访问控制器变量之前应使用别名。在vm.值内添加ng-model

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

10-04 16:32