Controller function not getting recognized in js file

47 views
Skip to first unread message

J2G

unread,
Dec 16, 2013, 6:14:34 AM12/16/13
to ang...@googlegroups.com

I have a Controller defined in my js file as below. It all works perfect until i change the placement of the "Test2Controller" controller in my javascript file. When i place the Test2Controller below the SampleController function / function declaration it gives me the below error :

Error in Chrome Dev tools :

Uncaught TypeError: Property 'controller' of object # is not a function mycontroller1.js:26 Error: Argument 'Test2Controller' is not a function, got undefined at assertArg (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:1039:11) at assertArgFn (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:1049:3) at file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4802:9 at file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4384:17 at forEach (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:137:20) at nodeLinkFn (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4369:11) at compositeLinkFn (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4015:15) at compositeLinkFn (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4018:13) at compositeLinkFn (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:4018:13) at publicLinkFn (file:///D:/Dev/olite/workspace/olite/olite/WebContent/AngularJSDemos/mylearnings/angular.js:3920:30) ...

Working ::

var myModule = angular.module ('mymodule',[]) ;

myModule.controller('TestController', function($scope) {
    $scope.message = 'Message from TestController';
});

myModule.controller('Test2Controller',function ($scope){
    $scope.message = "Message from Test2Controller" ; 
}) ;

myModule.controller = 'SampleController' ;

function SampleController( $scope ) {
    $scope.customers = [
                        {name:'AAA',city:'Plano'},
                        {name:'BBB',city:'Plano'},
                        {name:'CCC',city:'Bangalore'},
                        {name:'DDD',city:'SanJose'},
                        {name:'EEE',city:'SanFO'},
                        {name:'FFF',city:'SanJose'}
                        ] ;

    $scope.addCustomer = addCust ;

    function addCust( ) {
        $scope.customers.push ( {name:$scope.customerName , city:$scope.customerCity} ) ;
    } ;
}

Not working ::

var myModule = angular.module ('mymodule',[]) ;

myModule.controller('TestController', function($scope) {
    $scope.message = 'Message from TestController';
});

myModule.controller = 'SampleController' ;

myModule.controller('Test2Controller',function ($scope){
    $scope.message = "Message from Test2Controller" ; 
}) ;

function SampleController( $scope ) {
    $scope.customers = [
                        {name:'AAA',city:'Plano'},
                        {name:'BBB',city:'Plano'},
                        {name:'CCC',city:'Bangalore'},
                        {name:'DDD',city:'SanJose'},
                        {name:'EEE',city:'SanFO'},
                        {name:'FFF',city:'SanJose'}
                        ] ;

    $scope.addCustomer = addCust ;

    function addCust( ) {
        $scope.customers.push ( {name:$scope.customerName , city:$scope.customerCity} ) ;
    } ;
} 

I am currently learning angular js and kinda struck here. What could be the issue here ??

Daniel Tabuenca

unread,
Dec 16, 2013, 11:21:29 AM12/16/13
to ang...@googlegroups.com

Woah! Why are you doing:

myModule.controller = 'SampleController'

What you just did there is replace the myModule.controller() function with a string SampleController. This means next time you try to call myModule.controller('Test2Controller',.....) it won’t work because you just overwrote the function. What did you think doing myModule.controller='SampleController' did?

Naveen Krishnan

unread,
Dec 23, 2013, 6:15:39 AM12/23/13
to ang...@googlegroups.com
Thanks Daniel,

I got it now.




--
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/gMG_4EkEajY/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.

Reply all
Reply to author
Forward
0 new messages