Ich spiele mit angularjs
services
und factories
. Ich habe eine Fabrik BookFactory
wie folgt genannt erstellt:Angularjs: Erreiche die gleiche Struktur für den Service als Factory
// Code goes here
app
.factory('BootFactory', function() {
var bookFactory;
function add() {
}
function remove() {
}
function getAll() {
}
bookFactory = {
add: add,
remove: remove,
getAll: getAll
}
return bookFactory;
});
Und ein BookService
wie folgt:
app.service('BookService', function() {
var bookService;
function add() {
}
function remove() {
}
function getAll() {
}
/*
It throws:
Uncaught ReferenceError: Invalid left-hand side in assignment.
How can I achieve the same structure with the services which I have used in factories.
this = {
add: add,
remove: remove,
getAll: getAll
};
*/
//Do I have to add all methods like this
this.add=add;
this.remove=remove;
this.getAll=getgAll;
//Or there's any other better way to achieve it
});
Was soll ich tun, um die ich die die Struktur konsistent dh halten halten will ich will etwas wollen so für die Fabrik und Dienstleistungen:
Im Falle der Fabrik funktioniert es gut. Aber im Servicefall kann ich das nicht tun. Ursache Service arbeitet mit this
und ich kann das nicht tun:
/*
It throws:
Uncaught ReferenceError: Invalid left-hand side in assignment.
How can I achieve the same structure with the services which I have used in factories.
this = {
add: add,
remove: remove,
getAll: getAll
};
*/
Was ich denke, ist zu tun:
//Do I have to add all methods like this
this.add=add;
this.remove=remove;
this.getAll=getgAll;
//Or there's any other better way to achieve it
Gibt es einen besseren Weg, dies zu tun? Hier ist die .
Wir sollten nichts mit der Service-Funktion zurückgeben, da es die Konstruktorfunktion ist. –
Yeap, richtig. Ein Service ist eine Konstruktorfunktion, die uns jedoch nicht daran hindert, zusätzliche Arbeit zu leisten und Objektliterale zurückzugeben. Tatsächlich können Konstruktorfunktionen in JavaScript zurückgeben, was sie wollen. So können wir unseren Service-Code nehmen und schreiben es auf eine Weise, dass es im Grunde genau das Gleiche wie unsere Fabrik tut –
@HiteshKumar Lesen Sie dies bitte http://blog.thoughtram.io/angular/2015/07/07/service -vs-factory-einmal und für alle.html –