Update scope value when service data is changed

后端 未结 6 1252
天命终不由人
天命终不由人 2020-11-27 10:57

I have the following service in my app:

uaInProgressApp.factory(\'uaProgressService\', 
    function(uaApiInterface, $timeout, $rootScope){
        var facto         


        
6条回答
  •  醉酒成梦
    2020-11-27 11:49

    I'm not sure if thats help but what I am doing is bind the function to $scope.value. For example

    angular
      .module("testApp", [])
      .service("myDataService", function(){
        this.dataContainer = {
          valA : "car",
          valB : "bike"
        }
      })
      .controller("testCtrl", [
        "$scope",
        "myDataService",
        function($scope, myDataService){
          $scope.data = function(){
            return myDataService.dataContainer;
          };
      }]);
    

    Then I just bind it in DOM as

  • This way you can avoid to using $watch in your code.

提交回复
热议问题