ตั้งค่าบริการของคุณเป็นผู้ให้บริการ AngularJS ที่กำหนดเอง
แม้จะมีคำตอบที่ยอมรับแล้ว แต่คุณสามารถทำสิ่งที่คุณตั้งใจจะทำได้ แต่คุณต้องตั้งค่าเป็นผู้ให้บริการที่กำหนดค่าได้เพื่อให้บริการในช่วงระยะเวลาการตั้งค่า .. ขั้นแรกให้เปลี่ยนService
เป็นผู้ให้บริการ ดังแสดงด้านล่าง ความแตกต่างที่สำคัญที่นี่คือหลังจากตั้งค่าdefer
แล้วคุณตั้งค่าdefer.promise
คุณสมบัติเป็นวัตถุสัญญาที่ส่งคืนโดย$http.get
:
ผู้ให้บริการ: (ผู้ให้บริการ: สูตรบริการ)
app.provider('dbService', function dbServiceProvider() {
//the provider recipe for services require you specify a $get function
this.$get= ['dbhost',function dbServiceFactory(dbhost){
// return the factory as a provider
// that is available during the configuration phase
return new DbService(dbhost);
}]
});
function DbService(dbhost){
var status;
this.setUrl = function(url){
dbhost = url;
}
this.getData = function($http) {
return $http.get(dbhost+'db.php/score/getData')
.success(function(data){
// handle any special stuff here, I would suggest the following:
status = 'ok';
status.data = data;
})
.error(function(message){
status = 'error';
status.message = message;
})
.then(function(){
// now we return an object with data or information about error
// for special handling inside your application configuration
return status;
})
}
}
ตอนนี้คุณมีผู้ให้บริการที่กำหนดเองที่กำหนดค่าได้คุณเพียงแค่ต้องทำการฉีดเท่านั้น ความแตกต่างสำคัญที่นี่คือ "ผู้ให้บริการฉีดของคุณ" ที่หายไป
การตั้งค่า:
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: "partials/editor.html",
controller: "AppCtrl",
resolve: {
dbData: function(DbService, $http) {
/*
*dbServiceProvider returns a dbService instance to your app whenever
* needed, and this instance is setup internally with a promise,
* so you don't need to worry about $q and all that
*/
return DbService('http://dbhost.com').getData();
}
}
})
});
ใช้ข้อมูลที่ได้รับการแก้ไขใน appCtrl
app.controller('appCtrl',function(dbData, DbService){
$scope.dbData = dbData;
// You can also create and use another instance of the dbService here...
// to do whatever you programmed it to do, by adding functions inside the
// constructor DbService(), the following assumes you added
// a rmUser(userObj) function in the factory
$scope.removeDbUser = function(user){
DbService.rmUser(user);
}
})
ทางเลือกที่เป็นไปได้
ทางเลือกต่อไปนี้เป็นวิธีการที่คล้ายกัน แต่อนุญาตให้คำจำกัดความเกิดขึ้นภายในการ.config
ห่อหุ้มบริการภายในโมดูลที่ระบุในบริบทของแอปของคุณ เลือกวิธีที่เหมาะสมกับคุณ ดูด้านล่างเพื่อดูบันทึกย่อในลิงค์ที่เป็นทางเลือกที่ 3 และลิงค์ที่มีประโยชน์เพื่อช่วยให้คุณไม่พลาดทุกสิ่งเหล่านี้
app.config(function($routeProvider, $provide) {
$provide.service('dbService',function(){})
//set up your service inside the module's config.
$routeProvider
.when('/', {
templateUrl: "partials/editor.html",
controller: "AppCtrl",
resolve: {
data:
}
})
});
ทรัพยากรที่มีประโยชน์
- John Lindquist มีคำอธิบายที่ยอดเยี่ยม 5 นาทีและสาธิตสิ่งนี้ที่egghead.ioและเป็นหนึ่งในบทเรียนฟรี! ฉันปรับเปลี่ยนการสาธิตของเขาโดยการทำให้เป็นเรื่อง
$http
เฉพาะในบริบทของคำขอนี้
- ดูคู่มือนักพัฒนา AngularJS เกี่ยวกับผู้ให้บริการ
- นอกจากนี้ยังมีคำอธิบายที่ดีเกี่ยวกับ
factory
/ service
/ provider
ที่ clevertech.biz
ผู้ให้บริการให้การกำหนดค่าอีกเล็กน้อยสำหรับ.service
วิธีการซึ่งจะทำให้ดีขึ้นในฐานะผู้ให้บริการระดับแอปพลิเคชัน แต่คุณสามารถห่อหุ้มสิ่งนี้ภายในวัตถุการกำหนดค่าเองโดยการฉีด$provide
เข้าสู่การกำหนดค่าดังนี้