non scope functions and variables

193 views
Skip to first unread message

Jake K.

unread,
Jan 8, 2014, 8:30:53 PM1/8/14
to ang...@googlegroups.com

Hello

I was wondering what is the proper way of declaring functions and variables inside controllers that are not part
of the scope, for instance:


function controller( $scope ){

 $scope.var;

 var myvar = 'Hello world';

 var myfunction = function(){
  alert('Hello world');
 }

}

How should I properly declare myvar and myfunction inside a controller?

Thank you

PS: The reason why I do this is because some variables inside my controllers act more like helpers, and 
do not need to be bound or displayed in the view.

Thanks

Luke Kende

unread,
Jan 9, 2014, 12:41:30 AM1/9/14
to ang...@googlegroups.com
What you have works fine.  If you need to share something between controllers, consider moving it a service.   If you are doing UI manipulation with jQuery, consider making a directive.  

Daniel Tabuenca

unread,
Jan 9, 2014, 9:31:05 AM1/9/14
to ang...@googlegroups.com

Declaring private/internal functions and variables like you did is perfectly legitimate use of Javascript. You can even declare functions like this:

function controller( $scope ){
 $scope.var;
 var myvar = 'Hello world'
;

 function myfunction(){
  alert('Hello world');
 }

}

Sander Elias

unread,
Jan 9, 2014, 10:05:14 AM1/9/14
to ang...@googlegroups.com

Hi All,

Ok, maybe I’m weird, but doesn’t anybody see the obvious bug in here ;)

function controller( $scope ){ 
   $scope.var; 
   var myvar = 'Hello world'; 
   function myfunction(){ 
       alert(myvar);  // <----- I assume this is the needed code for this brilliant piece!!
   }
 }

On a serious note,
Yeah, this is perfectly valid, and I my opinion preferable above putting everything on the $scope!

Regards
Sander

Jake K.

unread,
Jan 9, 2014, 12:14:44 PM1/9/14
to ang...@googlegroups.com
Thanks for all your answers,

I asked this question because sometimes I see sample code
with the keyword 'this' used for non-scope variables.



2014/1/9 Sander Elias <sande...@gmail.com>

--
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/PyXtGO2R-HA/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/groups/opt_out.



--
JAKE KWON +1 (604) 655 3109
email: jyk...@gmail.com

Sander Elias

unread,
Jan 9, 2014, 12:36:45 PM1/9/14
to ang...@googlegroups.com

Jake,

That is something different! Probably those controllers are designed to be used with the controller-as constructor.
a sample say’s more then 1000 words:

js:
App.controller('somecontroller',function () {
   this.name='Sander Elias'
});

html/template:
<div ng-controller='somecontroller as person'>Name : {{person:name}}</div>

This is also a form you can use. Also you can mix and match it with the $scope style!

Regards
Sander

Reply all
Reply to author
Forward
0 new messages