最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular is undefined - Stack Overflow

programmeradmin4浏览0评论

So I'm learning angular and I have a very basic html with a js script and I keep getting 'angular is undefined' on line 1 of App.js. I have all of the angular scripts and my script in a Scripts folder. What am I missing?

index.html:

<!DOCTYPE html>
<html xmlns="" data-ng-app="appMain">
<head>
    <title>{{ Heading }}</title>
</head>
<body ng-controller="homePageViewModel">
    {{ Heading }}
    <button ng-click="SayHello()">Click me</button>

    <script src="Scripts/angular-min.js"></script>
    <script src="Scripts/angular-route.min.js"></script>
    <script src="Scripts/App.js"></script>
</body>
</html>

App.js

    var appMainModule = angular.module('appMain', []);

appMainModule.controller("homePageViewModel", function($scope, $http,     $location){ 
    $scope.Heading = "This is the heading";

    $scope.SayHello = function () {
        alert('Hello');
    }
});

Folder structure in VS:

So I'm learning angular and I have a very basic html with a js script and I keep getting 'angular is undefined' on line 1 of App.js. I have all of the angular scripts and my script in a Scripts folder. What am I missing?

index.html:

<!DOCTYPE html>
<html xmlns="http://www.w3/1999/xhtml" data-ng-app="appMain">
<head>
    <title>{{ Heading }}</title>
</head>
<body ng-controller="homePageViewModel">
    {{ Heading }}
    <button ng-click="SayHello()">Click me</button>

    <script src="Scripts/angular-min.js"></script>
    <script src="Scripts/angular-route.min.js"></script>
    <script src="Scripts/App.js"></script>
</body>
</html>

App.js

    var appMainModule = angular.module('appMain', []);

appMainModule.controller("homePageViewModel", function($scope, $http,     $location){ 
    $scope.Heading = "This is the heading";

    $scope.SayHello = function () {
        alert('Hello');
    }
});

Folder structure in VS:

Share Improve this question edited Aug 13, 2015 at 17:14 Anonymous asked Aug 13, 2015 at 16:49 AnonymousAnonymous 1,9792 gold badges28 silver badges39 bronze badges 6
  • 1 Have you checked the console if angular is actually loaded? – Chrillewoodz Commented Aug 13, 2015 at 16:51
  • Just to make sure, your angular*.js files are actually found inside a folder called Scripts, right? – blurfus Commented Aug 13, 2015 at 16:52
  • @ochi yes..Checked, double checked. – Anonymous Commented Aug 13, 2015 at 16:53
  • 1 Screenshot your apps file structure if seeing George's ment didn't answer you. – IfTrue Commented Aug 13, 2015 at 16:54
  • @GeorgeStocker That's the strangest thing...Could it have anything to do with the fact it's just an empty website folder in Visual Studio? I wouldn't that would have anything to do with it though. – Anonymous Commented Aug 13, 2015 at 16:55
 |  Show 1 more ment

4 Answers 4

Reset to default 8

your script is wrongly named it should be this

<script src="Scripts/angular.min.js"></script>

instead of this:

<script src="Scripts/angular-min.js"></script>

Look at your file name and look at your script name.

Given that I've copied your code into a codepen; and the only changes I've made are to reference the CDNJS versions of Angular and Angular Route; and I'm not seeing an issue.

Your issue lies elsewhere.

Specifically, if you're building this project, your output folders need the same relative path to Scripts. In other words, your directory structure expects the following to be true:

 - index.html
 |
  - Scripts
  |
   - app.js
   - angular-min.js
   - angular-route.js

Another issue could be that you don't have Copy to output Directory=true or don't have the angular-min.js set as Content (something to be copied to the output folder).

Here's the codepen changes:

<!DOCTYPE html>
<html xmlns="http://www.w3/1999/xhtml" data-ng-app="appMain">
<head>
    <title>{{ Heading }}</title>
</head>
<body ng-controller="homePageViewModel">
    {{ Heading }}
    <button ng-click="SayHello()">Click me</button>

    <script src="https://cdnjs.cloudflare./ajax/libs/angular.js/1.4.3/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare./ajax/libs/angular.js/1.4.3/angular-route.min.js"></script>
</body>
</html>

you need to set type="text/javascript" in your angular script definitions. try

<script type="text/javascript" src="Scripts/angular-min.js"></script>
<script type="text/javascript" src="Scripts/angular-route.min.js"></script>
<script type="text/javascript" src="Scripts/App.js"></script>

I faced the same issue about angular being undefined. In my code type="text/javascript" attribute was missing. After I added this attribute, it worked fine. Try this also if the solutions above do not work.

发布评论

评论列表(0)

  1. 暂无评论