2014-01-20 33 views
0

我有兩個控制器和設置一個服務以保存數據控制器之間傳遞多個對象的值:使用服務

var myApp = angular.module('myApp', []); 
myApp.factory('Data', function() { 
    return { message: "This is a message from a service", Type: "This is a type from a service" } 
}); 


function FirstCtrl($scope, Data) { 
    $scope.data = Data; 
} 

function SecondCtrl($scope, Data) { 
    $scope.data = Data; 
} 

在我的HTML,我有輸入結合這些值:

<div ng-controller="FirstCtrl"> 
     <input type="text" ng-model="data.message"> 
     <h1>{{ data.message }}</h1> 
    </div> 

    <div ng-controller="SecondCtrl"> 
     <input type="text" ng-model="data.type"> 
     <h1>{{ data.type }}</h1> 
    </div> 

但是,所有即時從服務退回的是我的data.message,並沒有任何內容data.type

這是爲什麼?

+1

它應該是data.Type - 只有一個錯字? – michael

+0

確實哈哈哈!謝謝! – Keva161

回答

0

這是一個簡單的類型錯誤。 Factory返回一個數據對象,其中鍵入'Type'。在你的HTML中,你使用'type'作爲鍵而不是'Type'。