变量未在AngularJS中定义

时间:2015-09-30 11:54:41

标签: javascript html angularjs

我刚开始玩AngularJS,我遇到了一个问题。我收到错误,我在Services.js的第5行告诉app is not defined。我环顾四周,了解到必须在功能之外定义应用程序,或者它不公开,所以我把它移出去了,但这没有帮助,也尝试移动脚本标签index.html到标题而不是Html之后。这是我的代码。

的index.html

<!DOCTYPE html>
<html data-ng-app="RESTClientModule">
<head>
    <title></title>
</head>
<body>
    <div ng-controller="AngularJs_WCFController">
        {{Returnresult}}
    </div>
</body>
</html>

<script src="../Scripts/angular.js"></script>
<script src="../Scripts/Test/Services.js"></script>
<script src="../Scripts/Test/Modules.js"></script>
<script src="../Scripts/Test/Controller.js"></script>

Modules.js

/// <reference path="../angular.js" />
/// <reference path="../angular.min.js" />  
var app;
(function () {
    app = angular.module("RESTClientModule", []);
})();

Services.js

/// <reference path="../angular.js" /> 
/// <reference path="../angular.min.js" />  
/// <reference path="Modules.js" />  

app.service("AngularJs_WCFService", function ($http) {

    this.GetData = function () {
        return $http.get("http://localhost:61794/Service1.svc/GetData");
    };

});

Controllers.js

/// <reference path="../angular.js" /> 
/// <reference path="../angular.min.js" />  
/// <reference path="Modules.js" />  
/// <reference path="Services.js" />  

app.controller("AngularJs_WCFController", function ($scope, $rootScope, $window, AngularJs_WCFService) {

    $scope.Returnresult = "none";

    function GetData() {
        var promiseGet = AngularJs_WCFService.GetData();
        promiseGet.then(function (pl) {
            $scope.Returnresult = pl.data
        },
             function (errorPl) {
             });
    }

});

1 个答案:

答案 0 :(得分:1)

如您所见,您在"Services.js"(App is not define here )之前添加了"Modules.js"(app define here ),您可以在其中定义您的应用。 因此,您应该在"Modules.js"之前加入"Service.js"来解决此问题

所以它应该:

<script src="../Scripts/angular.js"></script>
<script src="../Scripts/Test/Modules.js"></script>
<script src="../Scripts/Test/Services.js"></script>
<script src="../Scripts/Test/Controller.js"></script>