我在angularjs中创建模块化应用程序,我有两个模块home和模板。如何将一个模块的功能用于第二个模块
模块主页
angular.module('home',[]).controller('homeCtrl', function($scope){
//this function to templatesModule
$scope.useThisFunctionInTemplateCtrl = function(){}
})
模块模板
angular.module('templates',[]).controller('templatesCtrl', function($scope){
//here function from homeCtrl
$scope.functionFromhomeCtrl = function(){}
})
主app.js
angular.module('myApp',['home', 'templates']);
答案 0 :(得分:3)
您需要service在控制器之间共享信息:
angular.module('yourModule').factory('yourService', function(){
var self = this;
self.sharedFunction = function(){}
return self;
})
并将其注入您的控制器
angular.module('home',[]).controller('homeCtrl', function($scope, yourService){
//this function to templatesModule
$scope.useThisFunctionInTemplateCtrl = yourService.sharedFunction();
})
$ rootScope用于存储全局变量,否则应该避免使用。
答案 1 :(得分:0)
这不是好习惯,但可以满足您的需求。
您可以使用controllers
访问$rootScope
以上的功能。
从here
了解详情家庭控制器:
angular.module('home',[]).controller('homeCtrl', function($scope, $rootScope){
//this function to templatesModule
$rootScope.useThisFunctionInTemplateCtrl = function(){}
})
另一位控制人:
angular.module('templates',[]).controller('templatesCtrl', function($scope, $rootScope){
//here function from homeCtrl
$rootScope.useThisFunctionInTemplateCtrl(); //from another controller
$scope.functionFromhomeCtrl = function(){}
})
答案 2 :(得分:0)
您可以通过两种方式实现:
<强> 1。创建和使用AngularJS服务:
以下是如何创建AngularJS服务并在不同控制器中使用AngularJS Service Mehtod的完整示例:
//AngularJS Module
var app = angular.module("Demo", ["ngRoute"])
//AngularJS Route Config
app.config(function($routeProvider, $locationProvider) {
$routeProvider.caseInsensitiveMatch = true;
$routeProvider.when("/products/details/:id",
{
templateUrl: "Temaplates/details.html",
controller: "productDetailsController"
})
.when("/products/edit/:id",
{
templateUrl: "Temaplates/edit.html",
controller: "productEditController"
})
// AngularJS Service
app.factory('productService', function ($http, $routeParams) {
return {
getDataById: function () {
//return promise from here
return $http.get("http://localhost:43618/api/Products", {
params: { id: $routeParams.id }
});
}
};
});
// AngularJS Controllers Where Service Method has been used
app.controller("productDetailsController", function ($scope, $http, $routeParams, $location, productService) {
$scope.message = "Product Details";
productService.getDataById().then(function (response) {
$scope.product = response.data;
}, function (error) {
console.log("Error occured ", error);
});
});
.controller("productEditController", function ($scope, $http, $routeParams, $location, productService) {
$scope.message = "Edit Page";
$scope.product = {};
productService.getDataById().then(function (response) {
$scope.product = response.data;
}, function (error) {
console.log("Error occured ", error);
});
$scope.updateProduct = function (product) {
$http({
method: "PUT",
url: "http://localhost:43618/api/Products",
params: { id: $routeParams.id },
data: product
})
.success(function () {
alert("Product Updated Successfully");
$location.path('/products');
});
}
})
<强> 2。使用rootScope对象
但是使用rootScope对象时出现问题,那就是:每当刷新页面时,rootScope对象都将丢失。
因此,最好在rootScope上使用AngularJS服务