Answer the question
In order to leave comments, you need to log in
How to change data inside Angular 1.5 Component via external Service?
I am writing an application using Angular 1.5 components. The data is passed to the component from the outside through the resolve parameter, so I can display data from different sources in the same component. But I don't understand how I should proceed to change this data inside the component.
Let's say I have a User service that works with users via an API. When the state (and, accordingly, the component) is loaded, I call the Users.get () method, get the user data and pass it inside the component. For routing I use UI Router. For example:
//...
$stateProvider
.state('users', {
url: '/users',
component: 'formPage',
resolve: {
values: function(Users) {
return Users.get();
});
//...
// Внутри компонента formPage
//...
component('formPage', {
bindings: {
values: '<'
},
//...
Answer the question
In order to leave comments, you need to log in
On stackoverflow.com they answered me :
resolve: {
values: function(Users) {
return Users.get();
},
onUpdate: function(){
return Users.update.bind(Users);
}
);
bindings: {
values: '<',
onUpdate: '<'
},
$ctrl.onUpdate(data).then(...
It's a little strange that the component doesn't need to know about the service to send data.
You can push the action through component bindings {action: '&'}
if you plan to use this form with different data submission services.
But it's certainly very strange :)
If the component is not tied to any data service, then communication can be done through properties from the parent component (application / controller).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question