javascriptangularjsangularjs-directiveecmascript-6angularjs-controlleras

Two way isolated binding angular directives es6


I am having trouble figuring out how isolating scopes work. My code seems to work fine when i comment the scope part of the directive. Can anyone explain what I'm missing?

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

Solution

  • As you had alias for your controller inside directive, so you should use it before accessing controller's variables. Add vm. inside ng-model value.

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