Is there a way to not fill HTML with directives?

Asked

Viewed 218 times

2

Using the directive ng-click to catch an event by clicking one link would be something like:

<a ng-click="call('home')" href="#" title="ir para homepage">Home</a>

But this wouldn’t be the same thing as using onclick="call('home')"?

A good practice in web development is not to separate each document with its proper functionality? Avoiding that lot of CSS and Javascript inline?

It is possible, for example, to select an element in a similar way to the known $('foo').on('click', function(){}); i and. can access the element of the Javascript file and without filling the HTML of directives?

For example:

<a ng-model='abrir' href='#'>Abrir</a>

And in the JS:

$scope.abrir.on('click', function(){
   call('foo');
});
  • I think it’s nice to be able to see in the view code the function of a button, but if I don’t use something like Angularjs I have to use $('foo').on('click', ... to ensure that the event is signed after the component is rebuilt by an ajax request. Informing the button function in the view is like informing the source of a field value, {{pessoa.nome}}. This is not the same as mixing html and javascript in the same code. You are not worried for example with the code <input class="btn-primary" type="submit"..., is? This code has the same concept as your <a ng-click="call('home')"..., nay?

  • I also see no problems, it is more a doubt of someone who tired of hearing "separates html from Js".

  • 4

    Good practices are more or less like this: "Use UTF-8", "Don’t use Goto", "Don’t put Return in the middle of the function", "Don’t use CSS in HTML", "Don’t leave if without { }"... Honestly, you have to use every single thing where it’s best at the best time and that’s it, as long as you know what you’re doing. Good practices do not provide a lack of knowledge. On the other hand, if you hold the knowledge, no longer need the canned rules called "good practices", so in the end what is really worth understanding how it works, and make a beautiful application, while the guy of good practices is palpitating.

  • 2

    Great resposmentário @Bacco.

2 answers

1

The ng-click is different from onclick. The Angular Directive uses expressions of the framework itself, that is, they are in the context of the scope of the Angular (which is not directly accessible by the onclick).

And no, there is no Angular method that behaves like jQuery handlers, but you can use it in parallel to perform global scope expressions.

  • Equal in the sense of "soiling" the HTML, not that they both behave the same way. And about parallel use, as it would be?

  • Like any other Javascript code, just reference it in HTML. You will not have direct access to the variables and functions that are in the Angularjs code. To access them, you will need to walk a little walk (which sometimes, depending on the structure of the project, can be great).

1

Angular comes to extend the HTML, at first you may find that it is soiling, for me it makes it more declarative, in the $('item') model. on('click', does something); there in the js file, it can be organized for a programmer but for a designer it is hidden because in html there is no reference to the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.