2016-05-06 7 views
2

Ich habe ein Problem mit meinem Angular Service. Ich habe zwei Controller und einen Service. Grundsätzlich erhält der erste Controller Daten über AJAX-Aufruf und speichert diese Daten auf dem Dienst. Der zweite Controller greift dann über den Service auf diese Daten zu. Ich habe die Daten vom 1. Controller zum Service erfolgreich übergeben, aber wenn ich auf die Daten vom 2. Controller zugreife, gibt es nichts zurück.Controller kann nicht auf Angular Service-Variable zugreifen

Ich habe übrigens eine Ansicht mit 2 Controllern.

Dank

Dienst

app.service('SharedDataService', function() { 
    // Holds subtask that will be passed to other controllers 
    // if SharedDataService is invoke. 
    var _subTask = {}; 
    return { 
    subTask : _subTask 
    }; 
}); 

-Controller 1

app.controller('MainCategoryController',function($scope,$http,SharedDataService){ 

    $scope.loadSubtask = function(m_uid){ 
     $http({ 
      method: 'POST', 
      url: $locationProvider + 'query_stasks', 
      data: { 
       m_uid: m_uid 
      } 
     }).then(function successCallback(response) { 
       SharedDataService.subTask = response.data; 
      },function errorCallback(response){ 
     }); 

    } 

} 

-Controller 2

app.controller('SubTaskController',function($scope,$http,$location,$rootScope,SharedDataService){ 



    $scope.$watch('SharedDataService.subTask', function(newValue,oldValue){ 
     console.log("ni sud');"); 
     if (newValue !== oldValue) { 
      $scope.subTasks = newValue; 
     } 
     return SharedDataService.subTask; 
    }); 

}

Antwort

1

Da SharedDataService nicht auf $scope ist, das erste Argument der $watch method muss ein watchExpression function anstelle ein Angular expression string sein.

app.controller('SubTaskController',function($scope,$http,$location,$rootScope,SharedDataService){ 

    $scope.$watch(
     function watchExpression() {return SharedDataService.subTask}, 
     function listener (newValue,oldValue){ 
      console.log("ni sud"); 
      if (newValue !== oldValue) { 
       $scope.subTasks = newValue; 
      } 
     }  
    }); 

}); 

Weitere Informationen finden Sie unter AngularJS $rootScope.scope API Reference - $watch.

+0

Verstanden !! es hat funktioniert .. ich habe nie "watchExpression" benutzt – drake24

0

Sie haben eine Methode zu schreiben, um die Daten in Dienst zu speichern und die Daten aus dem Dienst

app.factory('SharedDataService', function() { 
    // Holds subtask that will be passed to other controllers 
    // if SharedDataService is invoke. 
    var _subTask = []; 
    function setSubTask = function(data){ 
    _subTask = data; 
    }; 
    return { 
    subTask : _subTask, 
    setSubTask:setSubTask 
    }; 
}); 

und Controller Aufruf

SharedDataService.setSubTask(response.data); 

geben die Daten zu setzen ...

0

try es

app.service('SharedDataService', function() { 
    this._subTask ={}; 
}); 

// keep code same 1st controller 
app.controller('MainCategoryController',function($scope,$http,SharedDataService){ 

$scope.loadSubtask = function(m_uid){ 
    $http({ 
     method: 'POST', 
     url: $locationProvider + 'query_stasks', 
     data: { 
      m_uid: m_uid 
     } 
    }).then(function successCallback(response) { 
      SharedDataService._subTask = response.data; 
     },function errorCallback(response){ 
    }); 

    } 

    } 

// 2nd controller 

    app.controller('SubTaskController',function($scope,$http,$location,$rootScope,SharedDataService){ 

console.log(SharedDataService._subTask); 
}); 
1

Vielleicht sollten Sie den Wert verwenden Objekt in s speichern ervice.In meinem Projekt, ich immer wie folgt:

Dann in Ihrem Controller. Sie sollten service.toogle_sub_task aufrufen, um Werte festzulegen und zu erhalten. Probier es einfach aus. Beste Wünsche.

0

Die beste Vorgehensweise besteht darin, alle $ http/resource Aufrufe in Services und nicht direkt in Controller oder Direktiven vorzunehmen. Dies macht den Code mehr Modul und weniger miteinander verknüpft.

Sie sollten idealerweise

app.factory('SharedDataService', function ($http) { 
     var subtasks = []; 
     var saveSubtasks = function(sub){ 
      subtasks = sub; 
      console.log(subtasks) 
     } 
     var tasks = { 
      loadSubtasks : function(m_uid){ 
      $http({ 
         method: 'POST', 
         url: $locationProvider + 'query_stasks', 
         data: { 
          m_uid: m_uid 
         } 
       }).then(function(data){ 
        saveSubtasks(data); 
       }); 
      }, 
      getSubtasks : function(){ 
      return subtasks; 
      } 
      } 
     return tasks; 
    }); 

und verwenden Sie es wie

app.controller('SharedDataService',function($scope,SharedDataService){ 
    $scope.load = function(val){ 
    SharedDataService.loadSubtasks(val); 
    } 
}); 

app.controller('SubTaskController',function($scope,SharedDataService){ 
    $scope.get = function(){ 
    $scope.subtasks = SharedDataService.getSubtasks(); 
    console.log($scope.subtasks); 

    } 
}); 
Verwandte Themen