There is a directive in which data is transmitted:

export default class MenuDir { constructor() { this.scope = { menu: '=', selected: '=' }, this.restrict = 'E'; this.controllerAs = 'menuCtrl'; this.controller = MenuDirCtrl; this.templateUrl = '/templates/menu-template.html' } } class MenuDirCtrl { constructor($scope) { this.$scope = $scope; this.data = $scope.menu; **setTimeout(() => {console.log($scope.menu); console.log(this); return 1}, 3000);** } selectMenu(menu) { this.$scope.selected = menu; } isSelected(menu) { return menu === this.$scope.selected; } } 

HTML:

 <app-dir> <menu-dir menu="appCtrl.data" selected="appCtrl.selected"></menu-dir> </app-dir> 

menu = "appCtrl.data" - receives data from the server and of course data appears after initialization, but when it is received it is transmitted but not redefined in this.data = $ scope.menu; and this.data remains empty, how to solve this problem without starting the digest cycle?

  • without the function of receiving anything, it is not clear, but most likely some left jQuery is used there - and without starting the digest cycle it is impossible to solve, because otherwise the angular will not know that something needs to be updated. Perhaps it is worth refusing the angulyar if its capabilities are not used - Grundy
  • But the point here is generally to use? Yes, and save it to the controller - Grundy
  • @Grundy parameter is passed to the directive through the attribute, menu: '=', but I use the syntax this and not the scope, and it turns out that this is not updated - Khotey Vitaliy
  • it's all because you use it wrong - Grundy
  • I am caching the scope in order to save its value in the parental directive - Khotey Vitaliy

1 answer 1

In the constructor directive add

 this.bindToController = true; 

And now in the controller directive menuCtrl there are properties

 this.scope = { menu: '=', selected: '=' }, 

Learn more http://blog.thoughtram.io/angularjs/2015/01/02/exploring-angular-1.3-bindToController.html

  • Please try to publish detailed answers containing a specific example of the minimum solution, supplementing them with a link to the source. Answers –references (as well as comments) do not add knowledge to the Runet. - Nicolas Chabanovsky