D
D
dmitrytut2014-03-07 22:17:02
Angular
dmitrytut, 2014-03-07 22:17:02

What is the best way to provide data to an AngularJS directive?

Hello!
The question is more from the category of "Best Practices". You need to write a directive in AngularJS. The directive is simple - input, below it is a line of text with some data and a button. The data is received from the server, when the button is pressed, the contents of input'a are sent to the server.
Question:
Where should I put the logic for getting data from the server?
You can do everything inside the directive in the link and controller, or you can pass data to the directive from the outside and logic, respectively, implement it every time, regardless of the directive.
Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan Starkov, 2014-03-08
@dmitrytut

So the code is clearer my-dir data-my-text="myText" data-my-model="myModel"
it already leads to the idea that the directive is not simple,
it changes the model, etc.
And since the code gives additional knowledge or a hint of this knowledge,
then such a code is better.
In terms of reusability of simple directives - very often they are one-time - so I would bother about this topic the least.

I
Ivan Starkov, 2014-03-07
@icelaba

In general, according to best practices, all sending and receiving data is placed in separate services :-) I store such services, for example, in a separate resources folder (not only me)
The directive, when defined in the header, indicates the dependence on this service
angular.module('directive_mod_blabla', [' resources.blabla']).
The reference https://github.com/angular-app/angular-app
(interaction through client/vendor/mongolab/mongolab-resource.js) was made according to a similar principle.
Then you can say a lot about testing, angular way, blah blah, etc. but you can also read it on the angular site.
Not according to best practice, just chain the module with a dependency on 'ngResource'
, pass ['$resource', function($resource)
and use
var blabla_resource = $resource('/blabla/:blabla_id', {blabla_id:'@blabla_id'})
.....
blabla_resource.get({blabla_id:blabla_id}).$promise
.then(function(res){} )
For a bunch of small tasks, this approach is no worse

D
dmitrytut, 2014-03-08
@dmitrytut

Thank you.
Now I have just done it in such a way that the logic of interaction with the server is placed in a separate service, and I "inject" it into the directive.
Now, after re-reading the question, I realized that I did not ask what I wanted ... The
real question is not about the logic of interaction with the server, but about the logic of providing data to the directive in general !!!
In a specific, particular case, the data is obtained from the server, i.e. question:
What is the best way to receive data inside the directive and use it simply:
Or, after all, leave the receipt of data at the mercy of the one who will use the directive, and transfer data to the directive through attributes:
Sorry for the inaccuracy in the formulation of the question, the question has been edited.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question