AngularJS : directives and scope

后端 未结 4 1601
无人及你
无人及你 2021-01-21 01:39

I have a simple question that I think has a pretty simple solution, but somehow I am missing it.

I\'m setting up a simple directive for my app. I\'m leaving the scope pr

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-21 01:44

    The problem in your code is: MachineServices.getByCustomerId and MachineServices.query are asynchronous. When you call these functions, data from the server does not arrive yet and these lines

      $scope.Machines = data.results;
      $scope.pages = data.pages;
    

    are not called yet.

    Scope in your link function is bound to the parent's scope. But when the link function is called, the parent's scope.pages is still undefined (due to asyn)

    When we work with angular data-binding, we should be passive, just listen for changes and react accordingly(use $watch, for example), because our code does not know and should not know when the bound property has value or has its value updated. Let angular notify us the changes

提交回复
热议问题