[英]unable to detect data with angularjs from json file
對於大多數人來說,這可能很容易,但是我剛開始使用angularjs。
我正在按照此示例在angularjs中創建數據模型。
就像下面的示例一樣,我正在創建一個包含所有與數據相關的操作的類,這樣我就可以調用相同的模型並在其他地方使用它。
.factory('DataTest', ['$http', function ($http) {
function DataTest(Data) {
if (Data) {
this.setData(Data);
}
};
DataTest.prototype = {
setData: function (Data) {
angular.extend(this, Data);
},
load: function (id){
var scope = this;
$http.get('json/data.json').success(function(Data){
scope.setData(Data);
});
},
delete: function(){
$http.delete();
},
update: function (){
$http.put();
}
};
return DataTest;
}])
當我像這樣在控制器中創建范圍時:
.controller('AppCtrl', ['$scope', 'DataTest', function ($scope, DataTest) {
$scope.test = new DataTest();
console.log($scope.test);
$scope.test.load();
}])
我的控制台中的結果如下所示:
DataTest{}
data: Array [7]
0: Object
1: Object
2: Object
3: Object
...
現在,我需要獲取內部data
並且嘗試了幾種方法,但都沒有碰到運氣:
$scope.test = new DataTest();
$scope.test2 = $scope.test.data;
console.log($scope.test2); //Console says 'Undefined'
$scope.test.load();
-----------------------------
$scope.test = new DataTest();
$scope.test2 = $scope.test.data;
console.log($scope.test2);
$scope.test.data.load(); //Console says 'Cannot read property load of undefined'
------------------------
Tried returning DataTest.data from my DataTest service, and console says:
//'Provider 'DataTest' must return a value from $get factory method.'
如何正確訪問測試范圍內的“數據”數組? 謝謝你的幫助
編輯
JSON文件(示例):
"tm": "2015-12-24T15:43:29+0100",
"data": [
{
"ID": 0,
"Type": 1,
"Name": "test 0"
},
{
"ID": 1,
"Type": 3,
"Name": "test 1"
},
{
"ID": 2,
"Type": 4,
"Name": "test 2"
}
],
"errors": []
請檢查您的json文件。是否准備好了json格式,也沒有。 試試這個:
數據[0] .something
它可能會獲取json數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.