0

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? Commented Jul 6, 2017 at 6:45
  • Check the folder path while referencing definition files path : /// <reference path="../app.js" /> Commented Jul 6, 2017 at 6:52

2 Answers 2

0

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.