Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将json文件与AngularJS中的单个控制器应用程序一起使用_Angularjs_Json - Fatal编程技术网

如何将json文件与AngularJS中的单个控制器应用程序一起使用

如何将json文件与AngularJS中的单个控制器应用程序一起使用,angularjs,json,Angularjs,Json,我不知道如何为json文件创建服务并在控制器中调用此服务。 我的应用程序中有一个控制器,因此必须保留。您必须从控制器呼叫您的服务 您的服务: angular.module('YOURAPP') .service("userAService", function ($http) { this.promise= function () { return $http.get('resources/json/data.json')

我不知道如何为json文件创建服务并在控制器中调用此服务。
我的应用程序中有一个控制器,因此必须保留。

您必须从控制器呼叫您的服务

您的服务:

angular.module('YOURAPP')
.service("userAService",  function ($http) {
                this.promise= function () {
                    return $http.get('resources/json/data.json')
                }
    });
然后在控制器功能中:

mycontroller = function(userAService){
     userAService.promise().then(
         function(response){
             //do what you want in your promise
             // try console.log(response.data) to see if you get your data.
         }
     )
}

您必须从控制器呼叫您的服务

您的服务:

angular.module('YOURAPP')
.service("userAService",  function ($http) {
                this.promise= function () {
                    return $http.get('resources/json/data.json')
                }
    });
然后在控制器功能中:

mycontroller = function(userAService){
     userAService.promise().then(
         function(response){
             //do what you want in your promise
             // try console.log(response.data) to see if you get your data.
         }
     )
}

尝试通过
运行代码片段
蓝色按钮运行此示例

基本上,您需要声明该服务,并通过
Angular.DI
请求它,最后调用该服务方法并等待其结果。 希望能有帮助

angular
.module('测试',[]))
.constant('API','https://jsonplaceholder.typicode.com')
.service('TestService',函数(API,$http){
this.getData=函数(){
返回$http
.get(API+“/照片”)
.然后(功能(响应){
返回响应数据;
})
};
})
.controller('TestCtrl',函数($scope,TestService){
//但决心是首选
测试服务
.getData()
.then(功能(数据){
$scope.album=数据;
})
;
})
;

尝试通过
运行代码片段
蓝色按钮运行此示例

基本上,您需要声明该服务,并通过
Angular.DI
请求它,最后调用该服务方法并等待其结果。 希望能有帮助

angular
.module('测试',[]))
.constant('API','https://jsonplaceholder.typicode.com')
.service('TestService',函数(API,$http){
this.getData=函数(){
返回$http
.get(API+“/照片”)
.然后(功能(响应){
返回响应数据;
})
};
})
.controller('TestCtrl',函数($scope,TestService){
//但决心是首选
测试服务
.getData()
.then(功能(数据){
$scope.album=数据;
})
;
})
;


你会遇到什么错误?我没有错误,我没有在屏幕上查看json文件内容。你在哪里调用
userAService
getUserData
?你会遇到什么错误?我没有错误,我没有在屏幕上查看json文件内容。你在哪里调用
userAService
getUserData