not to use watch events in angular controller**

On Thursday, May 8, 2014 11:32:01 AM UTC-4, Billy Figueroa wrote:
>
> Sander here is a quote from the most "complete" angular js book "ng-book" 
> (page 429)
>
> Note: we should never use $watch in a controller: It makes it difficult to 
> test the controller.
> We’re making an allowance here for the sake of illustration, and we’ll 
> move these watches
> into services later.
>
> This is not the first place I have read about that. Stackoverflow also has 
> plenty of suggestions by people saying not to do this and here is an 
> example of an article that was fairly easy to find that agrees
>
> http://www.benlesh.com/2013/10/title.html
>
> as far as that line if (element[0].name === 'appointment_type')
>
> for some reason I plnkr I had to do that instead of element.context.name  I 
> am reading a lot about angular and definitely try to take the "best 
> practices". Everywhere I read I am told to keep controllers short if 
> possible and only use them for scope related things. DOM manipulation 
> should happen in directives etc and not having watch events in controllers 
> is one of the things I keep seeing
>
> I plan on using a directive if I use watch events like you did the next 
> question is...it is easy to grab the attributes of an element in the link 
> function so that is why I did it that way. What other way can I grab the 
> attributes of an element when I have something like ng-change on it? can I 
> pass the event ($event) and grab the data from there?
>
> I agree what you have works and I take your advice very serious because 
> you are very knowledgeable on all kinda of topics here but I have read too 
> much that says not to use watch events in angular so how else can we do 
> this that satisfies not doing that but also I don't do what I am doing 
> since you don't think its a good idea
>
>
> On Thursday, May 8, 2014 11:16:57 AM UTC-4, Sander Elias wrote:
>>
>> Hi Billy,
>>
>> Have a second look at my last sample, there is no directive in there at 
>> all.
>> in your directive you have this line: if (element[0].name === 
>> 'appointment_type'). I think that line smells.
>> You have all the data you need already in your program available, and 
>> here you are querying the done to ask it for info?
>> a few lines above you did put this in?? Don’t use the DOM as a 
>> communication channel!
>>
>> I don't know who has told you that watchers in your controller are bad 
>> practice? It is not. I put in 2 really simple watches, and the current 
>> version of Angular can parse those at incredible speed, and this will 
>> only get better. 
>>
>> I won't go into the function() {}() deeply, but it's called an 
>> IIFE<http://benalman.com/news/2010/11/immediately-invoked-function-expression/>,
>>  
>> and those help to protect my code.
>>
>> Regards
>> Sander
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to