directive - template can't access controller var?

25 views
Skip to first unread message

Jonathan Price

unread,
Aug 28, 2014, 2:41:24 AM8/28/14
to ang...@googlegroups.com
I've got this fiddle to illustrate my problem:

http://jsfiddle.net/d34cv9a6/1/

It's a simple directive for a reusable select element.  I can't seem to access a 'this.var' that's defined in the controller from within the template?  Anyone tell me what I am doing wrong?

Sander Elias

unread,
Aug 28, 2014, 6:39:11 AM8/28/14
to ang...@googlegroups.com

The this var in the template is never a controller. It's in most cases the current scope, but don't depend/use it that way!
easiest way, use controllerAs, as shown in your adapted fiddle: http://jsfiddle.net/dyL6gLze/

Regards
Sander

Puritan Paul

unread,
Aug 28, 2014, 2:52:52 PM8/28/14
to ang...@googlegroups.com
Aha!  Thank you!


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/0VtRjYNl9vw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Puritan Paul

unread,
Aug 28, 2014, 3:22:01 PM8/28/14
to ang...@googlegroups.com
So, the .push calls in the fiddle were just a replacement for an $http call in my code.  Now that the controllerAs bit is working, I’m seeing a timing issue in my code.  I can see the http call returning the array I expect, but it isn’t making it into the template.  Here’s the replacement code for the to push lines:

                $http.get('index.cfm?action=api.getCustomers')
                    .success(function(customers) {$log.log(customers); this.customers = customers})
                    .error();

                $log.log("last log");
                $log.log(this.customers);


I’m seeing the  $log.log(customers)  from the http call show in console AFTER I see the ‘last log’ and the empty this.customers array.

Am I missing something about the compile timing?  I still don’t fully understand the best way to handle this.

Thanks again for the help.



On Aug 28, 2014, at 3:39 AM, Sander Elias <sande...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages