Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing model objects inside controllers to manipulate DOM - angular js- Best practices?

I hava a simple question after reading the post below and working for some time now on angular js.

the post : no dom manipulation from angular js controllers

the concerned point (from the post) : Do not use controllers to
Manipulate DOM — Controllers should contain only business logic. Putting any presentation logic into Controllers significantly affects its testability. Angular has databinding for most cases and directives to encapsulate manual DOM manipulation.


the question : if I have a simple angular app and on a button click i am calling a function of my controller. In that function I want to do some simple business logic and depending on that business logic output i want to hide/show a button.

What is the best possible way to do it.

My current way of doing this is : PLUNKER EXAMPLE (does this way of doing stuff breaks the law in the angular js realm. is that against testing? please correct me)

like image 471
ankur Avatar asked Mar 30 '14 09:03

ankur


People also ask

What is used to manipulate the DOM by creating elements in Angular?

Manipulating the DOM is easy with Angular. We can directly access the element by using ElementRef provided by @angular/core . If you manipulate it by using a directive (common use case) — most are likely to use nativeElement directly like this: It will correctly change the color of the element.

Can we create nested controllers in AngularJS?

Answer: Yes we can have nested controllers. The thing is it works in a hierarchical way while using a View.

What is Controlleras in AngularJS?

In AngularJS, a Controller is defined by a JavaScript constructor function that is used to augment the AngularJS Scope. Controllers can be attached to the DOM in different ways.


1 Answers

The Angular guidelines don't say "do not use controllers to manipulate the dom" in the sense that you should never have the dom change as the result of something in a controller - but rather that you should never in a controller directly modifiy the dom. Don't use document.getElementById, don't use $("#element"), etc - never talk TO the dom, let your model take care of that by binding the model to the dom.

When you find yourself in a situation that you want to use jQuery to talk to the dom directly in a controller then it's time to look at directives. http://docs.angularjs.org/guide/directive

And I agree with @tasseKATT, move the ng-click code on the reset button code into a function - even if just for sanity.

edit

Is this testable? Yes - much more so than putting things inline in HTML or relying on/modifying the DOM directly.

In this situation all you have to test is that your scope functions do what they are supposed to - and trust that the Angular folks have already tested the fact that their bindings work as documented. You don't have to test that ng-click calls your function, you have to test that when called with whatever parameters your function does what your spec says it should.

As far as it being easier to write this type of code inline or in document.ready, etc - that's just a part of getting used to the Angular way of doing things. Coming from years of doing bindings with jQuery it definitely takes some getting used to. I'd argue that it's harder to do it the 'old' way.

Checkout the posts on this site that deal with testing Angular - there are write-ups on testing controllers, directives, services, etc - really helped putting it all together for me. http://www.benlesh.com/2013/05/angularjs-unit-testing-controllers.html

like image 134
brianj Avatar answered Sep 19 '22 18:09

brianj