JS Error with Angular JS Module
NickName:Ninja Ask DateTime:2014-09-11T13:09:08

JS Error with Angular JS Module

I wrote a Module to keep track of all my helper functions (scripts/apps.js):

angular.module('app', ['ngResource']).run(function($scope){

$scope.UTIL = {

setup_pod_variables: function (pods){...}
...

});

I'm trying to call the helper function from my Controller defined in another directory: scripts/controller/Dashboard.js:

scope.UTIL.setup_pod_variables(pods);

I have included the other module:

var pods = angular.module('app', []);

But it seems that I am still getting an error:

TypeError: Cannot read property 'setup_pod_variables' of undefined
at new <anonymous> (file:///Users/simon_zhu/Documents/nautilus/app/scripts/controllers/Dashboard.js:16:12)
at d (file:///Users/simon_zhu/Documents/nautilus/app/scripts/angular.min.js:30:346)
at Object.instantiate (file:///Users/simon_zhu/Documents/nautilus/app/scripts/angular.min.js:30:475)
at file:///Users/simon_zhu/Documents/nautilus/app/scripts/angular.min.js:61:228
at file:///Users/simon_zhu/Documents/nautilus/app/scripts/angular.min.js:48:320
at q (file:///Users/simon_zhu/Documents/nautilus/app/scripts/angular.min.js:7:380)
at W (file:///Users/simon_zhu/Documents/nautilus/app/scripts/angular.min.js:48:186)
at f (file:///Users/simon_zhu/Documents/nautilus/app/scripts/angular.min.js:42:268)
at f (file:///Users/simon_zhu/Documents/nautilus/app/scripts/angular.min.js:42:285)
at f (file:///Users/simon_zhu/Documents/nautilus/app/scripts/angular.min.js:42:285) 

Any Ideas?

Copyright Notice:Content Author:「Ninja」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/25779501/js-error-with-angular-js-module

Answers
Rickard Staaf 2014-09-11T05:23:13

Utility functions and data is better placed in a service, then it can be accessed from anywhere.\n\nYour problem is that $scope in the module is not the same as the $scope in your controller.\n\nThe service solution would look something like this (might contain some errors as it is written from my head and not tested)\n\nangular.module('app',[]).factory('utils', function(){\n return { setup_pod_variables: function(pods){...}}; \n}\n\nangular.module('app').controller('myController', ['utils', function(utils){\n utils.setup_pod_variables(...); \n}])\n",


More about “JS Error with Angular JS Module” related questions

JS Error with Angular JS Module

I wrote a Module to keep track of all my helper functions (scripts/apps.js): angular.module('app', ['ngResource']).run(function($scope){ $scope.UTIL = { setup_pod_variables: function (pods){...}...

Show Detail

Angular JS Error: $injector:modulerr Module Error

I am creating a service in angular JS and trying to run but it is giving me error as below Failed to instantiate module myapp due to: Error: [$injector:nomod] Module 'myapp' is not available! You ...

Show Detail

Angular Js Error Adding Angular Module

I want to implement the application here, http://plnkr.co/edit/tOow1cCjrGiXxpeT3EXZ?p=preview Firstly, I use this application layout.html, &lt;script src="/Content/scripts/angular/angular.min.js...

Show Detail

Error loading angular module in node.js

I would like to use angular module in node.js and use some of the APIs like angular.copy(). I followed the instructions on https://www.npmjs.com/package/angular $ npm angular install --save I ad...

Show Detail

Error installing angular cli,module.js:549

I am trying to update Angular-CLI with $ npm install -g @angular/cli because it had many problems before.But now it's throwing the following error.Can someone help me? module.js:549 throw err; ^ ...

Show Detail

Module error in angular js

I am new to angular js I want progress bar line for file upload. I am getting below error when I am run my code: angular.js:38 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5...

Show Detail

Error loading external module in angular js 1.5.6

The error I see is: There is this equivalent question posted in SO: cannot load module in angular js That didn't help. I have the tag in my index.html &lt;script src="public/bower_components/ui.

Show Detail

angular js injector module error

i am new to angular js i am trying to inject factory in a config ,but it shows module not found factory code var app=angular.module('app',['ui.router','angular-jwt','ngResource','angularValida

Show Detail

Angular JS Unit Testing: search module error

In the package.json file I have following dependencies : "devDependencies": { "bower": "^1.7.7", "http-server": "^0.9.0", "jasmine-core": "^2.4.1",

Show Detail

Failed to Instantiate Module Angular JS

i am beginner in angularjs.i write a small code .i have three files. index.html &lt;!DOCTYPE html&gt; &lt;html ng-app="docsTransclusionDirective"&gt; &lt;head&gt; &lt;title&gt;Hel

Show Detail