簡單地說,角度服務總是讓我感到困惑,而我似乎通過反覆試驗讓他們工作。我想知道爲什麼在這種情況下數據返回未定義。簡單的角度服務混淆
應用模塊:
var app = angular.module('myApp', ['services']);
服務:
//creating a service module
var appServices = angular.module('services', []);
appServices.factory('Data', function(){
return {message : "I am data"}
});
控制器:
app.controller('firstCtrl',['Data',
function($scope, Data){
//get data from the service
$scope.data = Data;
console.log(Data);
}]);
app.controller('secondCtrl',['Data',
function($scope, Data){
//get data from the service
$scope.data = Data;
}]);
如果我控制檯日誌數據我只是得到 「未定義」。 我只是想做一個簡單的例子來返回對象{message:「我是數據」} ,以便
$ scope.data = Data;
然後在視圖
data.message =「我是數據」
會很感激的,爲什麼這是行不通的解釋。謝謝
謝謝,傻我:D –