Answer the question
In order to leave comments, you need to log in
Get object by id. Factory? Provider?
Angular 1.6 + ui-router No matter
how much I didn't delve into the essence of services, providers and factories, I didn't fully understand them...
As long as you need a singleton, everything is ok.
How to get some object from the server in ui-router in resolve by id?
It has an ES6 class with a bunch of methods. This object will still have to interact with other services.
class EntryModel {
constructor(data, $http, SomeService1, ...) { /* присваивание данных data и сохранение ссылок на сервисы */ }
myFunc() { ... }
}
angular.module('module').service('Entry', function($http, SomeService1, ...) {
return {
getById: function(id) {
return new Promise((resolve, reject) => {
$http.get(`/entries/${id}`).then(result => {
resolve(new EntryModel(result.data, $http, SomeService1, ...));
}, error => {
reject(error);
});
});
}
}
})
.config(function ($stateProvider) {
$stateProvider
.state('ide', {
url : '/{app:[0-9a-fA-F]{8}}',
component: 'ide',
resolve : {
entry: function (Entry, $stateParams) {
let entry = Entry.getById($stateParams.app);
return entry;
}
}
});
});
Answer the question
In order to leave comments, you need to log in
I advise you to immediately forget about the port of one to four. This is developer marketing.
when minified, such code will not work. you need to cut, for example, webpack's ngInject.
To make it easier, you can not use resolutions yet. IMHO dubious need piece. to make it work, you need to inject the resolve entry into the controller
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question