2014-09-29 57 views
1

//樣品如何通過HTTP

module.factory('$data', function() { 

      var data = {}; 
      data.items = [ 
       { 
        title: '1 Yet Another Item Title', 
        label: '4h', 
        desc: 'Lorem ...' 
       }, 

      ]; 

      return data; 
}); 
+1

你能否提供更多細節?而且如果可能的話,一個plunker例子 – rllola 2014-09-29 12:57:06

+0

它在遠程服務器上生成一個json文件,我需要讀這個遠程json – JeffB 2014-09-29 13:01:40

回答

0

你應該在你的控制器使用一個工廠從外部JSON中提取數據。我在這裏使用Firebase。

app.factory('Service', function(Firebase, $firebase, APP_CONSTANTS) 
{ 
var ref = new Firebase(APP_CONSTANTS.FIREBASE_URL); 
var angularFire = $firebase(ref); 

var speakers = angularFire.$child('speakers'); 
} 

其中APP_CONSTANTS.FIREBSE_URL只是Firebase的網址。

1

嘗試$http

$http.get('directory/file.json').success(function(response) { 
    return response.data; 
}); 

記住注入$ HTTP貴廠/服務

推薦文章:如果我得到你的意思Requesting JSON data with AJAX

0

,你想要的代碼是:

$json_content = file_get_contents("http://www.example.com/file.json"); 
$data = json_decode($json,true); 

另請參閱php.net manual爲json_decode, 你可以使用boolean $ assoc = true來關聯你的數據。 如果您希望json_decode的返回對象是一個簡單的數組,請使用json_decode($ json,false)或json_decode($ json)。