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;
});
}
Verstanden !! es hat funktioniert .. ich habe nie "watchExpression" benutzt – drake24