How to wait for binding in Angular 1.5 component (without $scope.$watch)

后端 未结 4 929
广开言路
广开言路 2021-01-31 08:24

I\'m writing an Angular 1.5 directive and I\'m running into an obnoxious issue with trying to manipulate bound data before it exists.

Here\'s my code:

ap         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-31 08:52

    I had a similar problem and I found this article very helpful. http://blog.thoughtram.io/angularjs/2016/03/29/exploring-angular-1.5-lifecycle-hooks.html

    I have an ajax call that hits the server on page load and my component needs the ajax return value to properly load. I implemented it this way:

    this.$onChanges = function (newObj) {
          if (newObj.returnValFromAJAX)
            this.returnValFromAJAX = newObj.returnValFromAJAX;
        };
    

    Now my component works perfectly. For reference I am using Angular 1.5.6

提交回复
热议问题