data.data 阅读我的 json
data.data for reading my json
我有这个工厂:
'use strict';
angular.module('testCon').factory('UserService', function ($http) {
return {
getAll: function () {
return $http.get('http://localhost:1337/api/user');
}
}
});
这个控制器:
'use strict';
angular.module('testCon').controller('UserController', function ($scope, UserService) {
$scope.users = [];
UserService.getAll().then(
function (data) {
$scope.users = data.data;
}, function (err) {
}
);
});
我能否以某种方式避免 data.data
我只想 data
。是什么迫使我做 data.data
以便在范围内看到它?
你无法避免这一点。您可以通过仅从 getAll
方法返回 response.data
来将其作为一次性更改。那么所有的消费者都会得到直接的数据。
代码
angular.module('testCon').factory('UserService', function ($http) {
return {
getAll: function () {
return $http.get('http://localhost:1337/api/user').then(function(response){
return response.data
});
}
}
});
您可以尝试这样的操作:
'use strict';
angular.module('testCon').factory('UserService', function ($http) {
return {
getAll: function () {
return $http.get('http://localhost:1337/api/user').then(function (response) {
return response.data;
});
}
}
});
然后将另一个函数保留为:
'use strict';
angular.module('testCon').controller('UserController', function ($scope, UserService) {
$scope.users = [];
UserService.getAll().then(
function (data) {
$scope.users = data;
}, function (err) {
}
);
});
第一个承诺 return 与第二个 then(..) 链接在一起,但第二个承诺获取提取的数据。关于承诺,这是一件很酷的事情。
我有这个工厂:
'use strict';
angular.module('testCon').factory('UserService', function ($http) {
return {
getAll: function () {
return $http.get('http://localhost:1337/api/user');
}
}
});
这个控制器:
'use strict';
angular.module('testCon').controller('UserController', function ($scope, UserService) {
$scope.users = [];
UserService.getAll().then(
function (data) {
$scope.users = data.data;
}, function (err) {
}
);
});
我能否以某种方式避免 data.data
我只想 data
。是什么迫使我做 data.data
以便在范围内看到它?
你无法避免这一点。您可以通过仅从 getAll
方法返回 response.data
来将其作为一次性更改。那么所有的消费者都会得到直接的数据。
代码
angular.module('testCon').factory('UserService', function ($http) {
return {
getAll: function () {
return $http.get('http://localhost:1337/api/user').then(function(response){
return response.data
});
}
}
});
您可以尝试这样的操作:
'use strict';
angular.module('testCon').factory('UserService', function ($http) {
return {
getAll: function () {
return $http.get('http://localhost:1337/api/user').then(function (response) {
return response.data;
});
}
}
});
然后将另一个函数保留为:
'use strict';
angular.module('testCon').controller('UserController', function ($scope, UserService) {
$scope.users = [];
UserService.getAll().then(
function (data) {
$scope.users = data;
}, function (err) {
}
);
});
第一个承诺 return 与第二个 then(..) 链接在一起,但第二个承诺获取提取的数据。关于承诺,这是一件很酷的事情。