将ID从一个控制器传递到不同的ng-app文件中的另一个 [英] Passing id from one controller to another of different ng-app files
问题描述
我有2个js文件,即collegeApp.js和branchApp.js.我有两个控制器CollegeController.js分别位于collegeApp.js ng-app内,而BranchController.js则位于branchApp.js ng-app内.
I have 2 js files namely collegeApp.js and branchApp.js. I have two controllers CollegeController.js which is inside collegeApp.js ng-app and BranchController.js which is inside branchApp.js ng-app.
我正在从html重定向到另一个页面.
From my html I am redirecting to another page.
这是我的 html
<li data-ng-click="getBranchByBranchId(branch.branchId); setBranchId(branch.branchId)">
{{branch.branchName}}
</li>
此html页面位于collegeApp.js中.单击分支名称后,我正在调用方法,其控制器如下所示.
This html page is in collegeApp.js.After clicking on branch name I am calling method And its controller looks like this.
CollegeController.js
var CollegeController = function($scope, $rootScope, $http, $location, $route,CollegeService,$routeParams) {
$rootScope.pageTitle = $route.current.title;
$scope.getBranchId = function() {
CollegeService.getBranchId().then(function(response) {
$scope.branchId = response.data;
});
}
$scope.setBranchId=function(branchId) {
CollegeService.setBranchId(branchId);
$rootScope.passBranchId = branchId;
window.location.href="./branch?
branchId='+$rootScope.passBranchId";//Here I am redirecting to branch page with id.//
}
}
分支页面在branchApp.js中,而上面的代码在collegeApp.js中.现在,在BranchController.js中,我试图捕获从上一页发送的branchId.
The branch page is in branchApp.js and above code is in collegeApp.js. Now in BranchController.js I am trying to catch branchId sent from previous page.
BranchController.js
var BranchController = function($scope, $rootScope, $http, $location, $route,BranchService,$routeParams)
{
$scope.branchId = $rootScope.passBranchId;//Here i am trying to get branchId//
console.log($scope.branchId);//I am getting undefined.
}
我尝试了$ rootScope,$ routeParams,但是没有一个起作用.有什么可能的方式可以将branchId从collegeApp传递到branchApp?还是我想念什么?
I tried $rootScope,$routeParams.But none of them worked. Is there any possible way that i can pass branchId from collegeApp to branchApp? or am i missing something?
推荐答案
我终于找到了解决方案.我刚刚添加了这一行,就可以了.
I finally found the solution.I just added this line and it worked.
Inside SchoolController.js
Inside SchoolController.js
$scope.setBranchId=function(branchId)
{
window.localStorage.setItem("branchId", branchId);
}
然后在BranchController.js中
And in BranchController.js
$scope.branchId = window.localStorage.getItem("branchId");
现在,我可以在控制器中的任何位置使用Id了,而且我还可以将Id从collegeApp.js传递到branchApp.js ng-apps.
Now i am able to use Id anywhere in controller and also i am able to pass Id from collegeApp.js to branchApp.js ng-apps.
这篇关于将ID从一个控制器传递到不同的ng-app文件中的另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!