0
votes

I created angular module in a javascript file app.js as below:

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

Further i went to the controller folder of my project and added a javascript file for Controllers. Next i referenced the already created app.js file(which is havning module creation) in the first step and then i tried to create controller using it as in below lines:

/// <reference path="app.js" />
app.controller('myctrl', function ($scope) {
$scope.tesdtata = 'test';
})

Now when i am binding the scope variable to UI and running the solution, i am getting exceptions as: 'app' not defined

Finally i am not able to vied the value of 'testdata' in browser.

Moreover when i type the line: app.controller(..) the intellisence shows me some diffenet overload variables i.e. app.controller(recepiename,factoryfunction) which is not correct.

Everything works fine if i create module individualy for every controller file and then using that create controller in same file. And problem occurs when i try to reference the single app.js file (having module creation it) and try to create controllers.

2
Can you show your HTML Code, the one where you have included all your dependencies and the one where u are trying to use the $scope variable? - Vivz
Check the folder path while referencing definition files path : /// <reference path="../app.js" /> - Ashish Kumar

2 Answers

0
votes

Check if your UI(HTML file) looks something like this. That'll do.

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="Scripts/angular.min.js"></script>
    <script src="app.js"></script>
    <script src="control.js"></script>
</head>
<body ng-controller="myCtrl">
        {{testdata}}

</body>
</html>
0
votes

It is discouraged to use global variables in javascript nowadays.

Instead of doing this

// app.js
var app = angular.module('myApp, []');

// controller.js
app.controller...

Do this

// app.js
var app = angular.module('myApp, []');

// controller.js
angular.module('myApp').controller...

Reference not needed and intellisense won't complain too.l