简体   繁体   中英

HTML page doesn't see AngularJS module

I am entirely new to AngularJS. The task is however simple: to parse XML file from the url and make a table out of it.

Yet somehow angular doesn't load. I searched similar questions, but neither worked. My index.html:

 <!doctype html>
<html ng-app="myApp">
<head>
<title>jj</title>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/angular.intellisense.js"></script>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/myscript.js"></script>

</head>
<body>
<div class="jumbotron">
    <h1>ASP.NET</h1>
    <p class="lead">Text.</p>

</div>
    <div ng-controller="AppController">


        <h3>for example 2+3= {{3+2}}</h3>

    </div>
 </div>
 </body>
 </html>

I should get 5 instead of 2+3 if angular is loaded? myscript.js currently looks like:

  angular.module('myApp.service', []).
myApp.factory('DataSource', ['$http', function ($http) {
    return {
        get: function(file,callback,transform){
            $http.get(
                file,
                {transformResponse:transform}
            ).
            success(function(data, status) {
                console.log("Request succeeded");
                callback(data);

            }).
            error(function(data, status) {
                console.log("Request failed " + status);

            });

        }           
    }
}]);

angular.module('myApp', ['myApp.service']);
var AppController = function ($scope, DataSource) {

var SOURCE_FILE = "guitars.xml";

xmlTransform = function (data) {
    console.log("transform data");
    var x2js = new X2JS();
    var json = x2js.xml_str2json(data);
    return json.customers.customer;
};    
setData = function (data) {
    $scope.dataSet = data;
 };
  DataSource.get(SOURCE_FILE, setData, xmlTransform);
 };

Can you give me some advice?

It has to do with your syntax. I believe you have 2 errors. In your myscript.js you are declaring 2 modules. In your first module you are incorrectly declaring a factory. Use .factory not myApp.factory

angular.module('myApp.service', [])
.factory('DataSource', ['$http', function ($http) {
  return {
    // do something
  }
}]);

In your second module you declare a function called AppController instead of calling the .controller angular method. Do this instead:

angular.module('myApp', ['myApp.service'])
.controller('AppController', ['$scope', 'DataSource', function ($scope, DataSource) {

  // controller code

}]);

You can accomplish your program in a sole module by using a construct similar to what follows:

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

app.controller('AppController', function($scope, DataSource) {
  // code
});

app.factory('DataSource', ['$http', function($http) {
  // code
}]);

Basically, create a variable for your app module, then call controller and factory, and pass your factory into your controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM