javascript - How do I handle interaction between the DOM and the Controller? -


I read (and tried to test) the angular application, I see it is a bad practice for a controller. Look at the DOM (for example).

I should be missing something big, because if the controller can not access DOM (like "regular" JavaScript calls like document.getElementsByClassName ), then I do not understand How to do a lot of things on which I think very basic.

Here's a simple and straightforward example, which has some problems that my app does:

  • I have an instruction that is just a red box (some styles , A division is applied), and uses ng-transcture, so I can call it my html file like & lt; Red-box & gt; Text that goes into the red box & lt; / Red-box & gt;
  • A button, when clicked, changes the color of all the red boxes in blue. Something like this will happen in my HTML file: & lt; Intput type = "button" value = "make them blue" ng-click = "makeThemBlue" />
  • In the makeThemBlue function of the controller, For all the divs I search by name of the class, and the class changes something else that makes them blue.

Now my real app is more complex that idea - with nested instructions Together many "boxes", which can be pulled around, and their position can be saved. The administrator reads all saved settings, and everything goes out according to how the user has saved it.

How can I do something like the above examples without getting the controller to Dome?

These are my main rules:

  1. / Code> - For concrete components and for dome manipulation.
  2. Services - Instructions, controllers, services, etc. should be used for business logic and saving status.
  3. Controller - A visual assistant should not have any business logic inside. Use a service for complex cases.

In your case, there should be a box director. .
Register your instruction for a noticeable service and click event.
When a click event occurs, the supervisor will notify all the registered directive instances that were clicked, and you should have your directive .


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -