在angularjs中注入模块中的多个模块

时间:2015-09-14 18:35:39

标签: angularjs

我有3个模块:

Services.js

var DataService = angular.module('DataService', [])
DataService.factory('DataOp', ['$http', 'dataShare' , function ($http, dataShare) {
  var DataOp = {};
DataOp.getReport = function(a,b){


    dt = a.getDate();
    mn = a.getMonth()+1;
    yr = a.getFullYear();
    sd = yr+"-"+mn+"-"+dt;

    dt = b.getDate();
    mn = b.getMonth()+1;
    yr = b.getFullYear();
    ed = yr+"-"+mn+"-"+dt;


    return  $http({method : "GET" , url : "http://localhost:8080/AnalyticsWeb/rest/report/dailyreport/",
       params :{"param1" : sd,"param2" : ed}});

     };

return DataOp;

}]);

DataService.factory('LoginService', ['$http', function($http) {
var factory = {};

factory.auth = function(a, b) {

return $http({method : "GET" , url : "http://localhost:8080/AnalyticsWeb/rest/report/login/",params :{"uname" : a,"password" : b}});

};
return factory;
 }]);

的script.js

var sampleApp = angular.module("sampleApp", ['ngRoute', 'DataService']);


sampleApp.controller("RouteController", function($scope, $location, LoginService, dataShare) {

    $scope.auth = function()
    {
         LoginService.auth($scope.username,$scope.password)
         .success(function (data) {

            $scope.response=data;

            var res= $scope.response.message;

            if($scope.response.message === "success") 
                {
                var path="/AnalyticsWeb/Daily.jsp";
                }

            else
                {
                var path="/AnalyticsWeb/index.html"
                }
            window.location.href=path;

         })
         .error(function (error) {
              $scope.status = 'Unable to authenticate: ' + error.message;
             alert($scope.status);
         });

        };
});

Controller.js

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

myApp.controller('DataController', function ($scope, DataOp) {
$scope.status;
$scope.reports;

$scope.getReports = function() {

    DataOp.getReport($scope.sdate,$scope.edate)
        .success(function (data) {
            $scope.reports = JSON.parse(data.report);

        })
        .error(function (error) {
            $scope.status = 'Unable to load customer data: ' + error.message;
        });
};

   });

现在成功登录后,我将路由到Daily.jsp页面。我想在controller.js(myApp)中使用数据(sampleApp中的RouterController)。请帮我知道我该怎么做。

我试图添加:

sampleApp.value("myValue",data);   in RouterController 

但是当我在myApp模块中添加SampleApp时,就像这样

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

甚至比DataController停止工作。没有'sampleApp'DataController正在工作。请告诉我为什么添加多个模块无法正常工作。

0 个答案:

没有答案