如何在angularjs中传递不同模块的控制器之间的值 [英] How to pass values between controllers of different module in angularjs

查看:202
本文介绍了如何在angularjs中传递不同模块的控制器之间的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我是MVC的新手,在使用angularjs时面临问题,同时将值从一个模块中的一个控制器传递到另一个模块中的其他控制器。这里有两个模块,即登录和myApp,所以基本上我想将数据从登录传递到myApp。



我尝试使用谷歌搜索工厂或服务,但它不是使用'ngRoute'或我正在使用的其他服务。

我突出了我需要传递给其他模块的区域。





如有任何帮助或建议,请发送电子邮件至vikasjbp@gmail.com,





提前致谢





LoginModule.js



var mylogin = angular.module(login,['ngRoute']);



mylogin.config( function($ routeProvider){

$ routeProvider.when(/ Home / Home,// Angular URL

{

templateUrl: / Home / Home,

控制器:ValidateLoginController

});

});



LoginController.js



mylogin.controller(ValidateLoginController,函数FunctionItem($ scope,$ window,$ http,$ routeParams){



$ scope.ValidateLogin = function(){

$ http.post(/ Login / ValidateLogin,$ scope.Login).then(function(e){

if(e.data .Success == true){

这里是在e.data中获取数据,需要将e.data传递给myApp



$ window.location.href =/ Home / Home;

}

else {

$ window.alert ('错误 - '+ e.data.Message +'。请再试一次!');

}

});

};



});





Module.js

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



myApp.controller(RouteCtrl,function($ scope, $ http,$ location,HomeDetails,UptimerobotAPI,$ rootScope,ngDialog,$ timeout,$ filter){





/ **创建$ scope.template ** /

$ scope.template = {

WebServer:/ Home / WebServer,

WebSite :/主页/网站

}





/ *获取服务器详细信息* /

var webserver = HomeDetails.getServers();



webserver.then(function(_server){

$ scope .WebServer = _server.data;

});

/ *服务器详细信息结束* /



/ *获取API数据* /

var _APIdata = UptimerobotAPI.CallUpTimeRobot();



_APIdata.then(function(_data){

var x2js = new X2JS();

$ scope.WebSite =(x2js.xml_str2json(_data.data))。monitors.monitor;



var urlBase ='/ Home / getAPIData';

var response = $ http({

method:post,

url :urlBase,

数据:$ scope.WebSite

});



response.then(function( r){

if(r.data.Success == true){

调试器;



}

},函数(ex){alert(ex.data)});

});

/ * API数据结束* /





/ * popup * /

$ scope.openTimed = function(_name){



$ http({

方法:'POST',

url:'/ Home / getLastDownTime',

数据:{url:_name}

})

.success(函数(数据){

var lastdowntime =;

var lastUPtime =;





if(data.LastDownTime!= null){

lastdowntime = $ filter('date')(parseInt(data.LastDownTime。 substr(6)),'dd-MM-yyyy hh:mm:ss');

}

else {

lastdowntime =没有记录停机时间!!!

}

if(data.LastUPTime!= null){

lastUPtime = $ filter('date ')(parseInt(data.LastUPTime.substr(6)),'dd-MM-yyyy hh:mm:ss');

}

else {

lastUPtime =没有UP时间记录!!!

}





var dialog = ngDialog.open({

template:'

Last DownTime - '+ lastdowntime +'

Up up - '+ lastUPtime +'    ('+ data.UPSince +')'+'

',

plain:true,

closeByDocument:false,

closeByEscape:false

});

setTimeout(function(){

dialog.close();

},2000);

});



}; < br $>


$ scope.openConfirm = function(_name){

调试器;

var myURL = _name;

$ scope.myURL = _name;

$ http({

方法:'POST',

url:' / Home / getLogbyName',

data:{_ name:_name}

})

.success(function(data){

$ rootScope.LogsByName = data;

ngDialog.openConfirm({

模板:'modalDialogId',

className:'ngdialog-theme-default'

})。then(function(value){

console.log('模态承诺已解决。价值:',价值);

},功能(原因){

console.log('模态承诺被拒绝。原因:',原因);

});

});

};

/ *结束弹出* /

});

解决方案

< blockquote> routeProvider){


routeProvider.when(/ Home / Home,// Angular URL

{

templateUrl:/ Home / Home,

控制器:ValidateLoginController

});

});



LoginController.js



mylogin.controller( ValidateLoginController,函数FunctionItem(


scope,


Folks, I am new to MVC with angularjs and facing issue while passing values from one controller in one module to other controller in other module. Here are two module ie login and myApp, so basically I want to pass data from login to myApp.

I tried with factory or service as per google search but its not working with 'ngRoute' or other services which I am using.
I highlighted the area where I am getting value need to pass to other module.


Any help or suggestion would be appreciated or mail me at vikasjbp@gmail.com,


Thanks in advance


LoginModule.js

var mylogin = angular.module("login", ['ngRoute']);

mylogin.config(function ($routeProvider) {
$routeProvider.when("/Home/Home", // Angular URL
{
templateUrl: "/Home/Home",
controller: "ValidateLoginController"
});
});

LoginController.js

mylogin.controller("ValidateLoginController", function FunctionItem($scope, $window, $http, $routeParams) {

$scope.ValidateLogin = function () {
$http.post("/Login/ValidateLogin", $scope.Login).then(function (e) {
if (e.data.Success == true) {
here am getting data in e.data and need to pass e.data to myApp

$window.location.href = "/Home/Home";
}
else {
$window.alert('Error - ' + e.data.Message + ' . Please try again!!!');
}
});
};

});


Module.js
var myApp = angular.module('myApp', ["ngRoute", "ngDialog" ]);

myApp.controller("RouteCtrl", function ($scope, $http, $location, HomeDetails, UptimerobotAPI, $rootScope, ngDialog, $timeout, $filter) {


/** create $scope.template **/
$scope.template = {
"WebServer": "/Home/WebServer",
"WebSite": "/Home/WebSites"
}


/*get server details*/
var webserver = HomeDetails.getServers();

webserver.then(function (_server) {
$scope.WebServer = _server.data;
});
/*server details end*/

/*get API data*/
var _APIdata = UptimerobotAPI.CallUpTimeRobot();

_APIdata.then(function (_data) {
var x2js = new X2JS();
$scope.WebSite = (x2js.xml_str2json(_data.data)).monitors.monitor;

var urlBase = '/Home/getAPIData';
var response = $http({
method: "post",
url: urlBase,
data: $scope.WebSite
});

response.then(function (r) {
if (r.data.Success == true) {
debugger;

}
}, function (ex) { alert(ex.data) });
});
/*API data end*/


/*popup*/
$scope.openTimed = function (_name) {

$http({
method: 'POST',
url: '/Home/getLastDownTime',
data: { url: _name }
})
.success(function (data) {
var lastdowntime = "";
var lastUPtime = "";


if (data.LastDownTime != null) {
lastdowntime = $filter('date')(parseInt(data.LastDownTime.substr(6)), 'dd-MM-yyyy hh:mm:ss');
}
else {
lastdowntime = "No Down Time recorded!!!"
}
if (data.LastUPTime != null) {
lastUPtime = $filter('date')(parseInt(data.LastUPTime.substr(6)), 'dd-MM-yyyy hh:mm:ss');
}
else {
lastUPtime = "No UP Time recorded!!!"
}


var dialog = ngDialog.open({
template: '

Last DownTime -' + lastdowntime + '

Up Since - ' + lastUPtime + '    (' + data.UPSince + ')' + '

',
plain: true,
closeByDocument: false,
closeByEscape: false
});
setTimeout(function () {
dialog.close();
}, 2000);
});

};

$scope.openConfirm = function (_name) {
debugger;
var myURL = _name;
$scope.myURL = _name;
$http({
method: 'POST',
url: '/Home/getLogbyName',
data: { _name: _name }
})
.success(function (data) {
$rootScope.LogsByName = data;
ngDialog.openConfirm({
template: 'modalDialogId',
className: 'ngdialog-theme-default'
}).then(function (value) {
console.log('Modal promise resolved. Value: ', value);
}, function (reason) {
console.log('Modal promise rejected. Reason: ', reason);
});
});
};
/*End popup*/
});

解决方案

routeProvider) {


routeProvider.when("/Home/Home", // Angular URL
{
templateUrl: "/Home/Home",
controller: "ValidateLoginController"
});
});

LoginController.js

mylogin.controller("ValidateLoginController", function FunctionItem(


scope,


这篇关于如何在angularjs中传递不同模块的控制器之间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆