gpt4 book ai didi

javascript - AJAX Angular 服务的揭示模块

转载 作者:行者123 更新时间:2023-12-03 12:30:41 25 4
gpt4 key购买 nike

下面我有一个 Angular 应用程序和 Controller ,其中 Controller 可以在其中访问数据(我知道这是个坏主意)

var app = angular.module('app',[]);
app.controller('HomeController',function($scope,$http){
$scope.people = null;
$scope.get = function() {
$http({
url: 'largeTestData.json',
method: 'GET'
}).then(function(data){
console.log('request successful, here is your data: ');
console.log(data['data']);
$scope.people = data['data'];
},function(reason){
console.log('this failed, this is the reason: ');
console.log(reason);
})

}

});
app.controller('ControllerWithService',function($scope, MyService){
$scope.get = MyService.get;
$scope.get(function(data){
console.log('you succeeded');
},function(reason){
console.log('you failed');
console.log(reason);
})

})

这将用于检索数据并将其放入页面上。知道在 Controller 中进行数据访问并不好,我尝试将其抽象为服务:

app.service('MyService',function($http,$q){
var get = function(){
var deferred = $q.defer();
var url = 'test.json';
$http.get(url).success(deferred.resolve).error(deferred.reject);
}
return {
get: get
}
})

这里我的“数据层”是一项只有一种方法的服务:从上面列出的 URL 获取。

app.service('MyService',function($http,$q){
var get = function(){
var deferred = $q.defer();
var url = 'test.json';
$http.get(url).success(deferred.resolve).error(deferred.reject);
}
return {
get: get
}
})

和我的 HTML

<body>
<script src="libs/angular-1.2.15.js"></script>
<script src="app/app.js"></script>
<script src="app/DocumentService.js"></script>
<script src="libs/jQuery-2.1.1.js"></script>
<div ng-controller="HomeController">
<button ng-click="get()" href="#">Get data</button>
<div>{{message}}</div>
<!--<div ng-repeat="p in people" >-->
<!--<b>Business Doc ID: </b><h1>{{p['busDocId']}}</h1>-->
<!--<b>DOC ID: </b><a href="#">{{p['docId']}}</a>-->
<!--<b>FILE NAME: </b><div style="color: green">{{p['fileName']}}</div>-->
<!--</div>-->

</div>
<div ng-controller="ControllerWithService">
{{message}}
<button ng-click="get()">get data</button>
<div>{{data}}</div>

</div>

</body>

我没有收到任何错误消息,并且 HomeController 中注释掉的内容按预期工作。我在尝试让 AJAX 调用服务时做错了什么?

工作解决方案更改:

    app.service('MyService',function($http,$q){
this.get = function(){
return $http.get('test.json')
}
})


app.controller('ControllerWithService',function($scope, MyService){
$scope.data = null;

$scope.get = function() {
MyService.get().then(function (data) {
console.log('this is the success data: ');
console.log(data)
$scope.data = data;
}, function (reason) {
console.log('this is the fail reason');
console.log(reason);
$scope.data = reason;
})
}
})

最佳答案

看起来可能是几个不同的事情。我将发布一个我现在在我的项目之一中工作的示例。它应该与您的目标非常相似且简单。

服务:

'use strict';
angular.module('srcApp')
.service('Getlanguage', function Getlanguage($location, $http, $log, $state, $rootScope) {

this.getContent = function() {
var language = $location.path().split('/'),
languageCulture = language[1];

if (!languageCulture) {
languageCulture = 'en';
}

$rootScope.cultureCode = languageCulture;

return $http({method: 'GET', url: '/languages/' + languageCulture + '.json'})
.error(function() {
// If service cannot find language json file, redirect to index
$state.go('lang', {lang: 'en'});
});
};


});

Controller 调用服务:将服务作为依赖项传递到 Controller 后。

 Getlanguage.getContent().then(function(res) {
$scope.content = res.data;
});

希望这有帮助。

关于javascript - AJAX Angular 服务的揭示模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23942929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com