如果我需要使用来自另一个模块的工厂,我是否需要首先将该模块的DI添加到我的当前模块,然后将该工厂的DI添加到当前工厂?或者我可以只添加工厂本身(没有它的模块)?
因此,如果上面是真的,那么Di在模块中的唯一用法就是用于那个用途……或者我还漏掉了什么?
发布于 2015-05-16 16:46:44
var myApp = angular.module('myApp', []);
myApp.service('myService', function() {
// do some stuff
});
myApp.controller('otherCtrl', function($scope, myService) {
// do some stuff
});
将myApp模块注入otherApp模块,并使用服务myService:
var otherApp = angular.module('otherApp', ['myApp']);
otherApp.controller('myCtrl', function($scope, myService) {
$scope.myService = myService;
});
发布于 2015-05-16 18:29:55
声明带有依赖项的模块。
var baseApp = angular.module("ERMSApp", ['ngSanitize', 'ngRoute', 'ngTable']);
var baseApp1 = angular.module("ERMSApp1", ['ERMSApp', 'ngSanitize', 'ngRoute', 'ngTable']);
声明服务。
baseApp.factory("getEmployeesService", function ($http) {
var promise;
var getEmployeesService = {
getEmployees: function () {
if (!promise) {
var promise = $http.get("/Timesheet/GetEmployees").then(function (result) {
return result;
});
}
return promise;
}
}
return getEmployeesService;
});
在另一个模块中使用服务
baseApp1.controller("leaveOnBehalfCtrl", function ($scope, $http, $filter, $sce, ngTableParams, $compile, getEmployeesService) {
getEmployeesService.getEmployees().then(function (data) {
$scope.employees = data.data;
})
});
https://stackoverflow.com/questions/30273486
复制相似问题