[英]Call function in the main module's $scope in Angular.js
I have an Angular.js project and the main module defined like this: 我有一个Angular.js项目和定义的主要模块,如下所示:
var app = angular.module('app', [ /* Some libraries... */
'UserControllers' ]);
The main module has a controller define like this: 主模块有一个控制器定义,如下所示:
app.controller('MainController', function($scope, $location) {
var UserId = null;
var Username = null;
$scope.setUserInfo = function(userId, username) {
UserId = userId;
Username = username;
};
}
In the main page of the website the user can load an external login form and load it into a modal like this: 在网站的主页上,用户可以加载外部登录表单并将其加载为如下形式的模式:
function showModal($uibModal, $previousState) {
$previousState.memo("modalInvoker");
var stateName = this.name;
var templateUrl = this.templateUrl;
$uibModal.open({
templateUrl: templateUrl,
controller: function ($uibModalInstance, $scope) {
var isopen = true;
$uibModalInstance.result.finally(function() {
isopen = false;
$previousState.go("modalInvoker");
});
$scope.close = function () {
$uibModalInstance.dismiss('close');
};
$scope.$on("$stateChangeStart", function(evt, toState) {
if(!toState.$$state().includes[stateName]) {
$uibModalInstance.dismiss('close');
}
});
}
});
}
The form in the loaded page uses a controller defined in a file loaded in the home-page like this: 加载页面中的表单使用在主页中加载的文件中定义的控制器,如下所示:
(function() {
"use strict";
var login = angular.module("UserControllers", [ ]);
login.controller("LoginCtrl", function($http, $scope) {
/* Login checks and requests... */
/* Need to call the setUserInfo */
}
}();
After doing all the login checks I basically need to call the function setUserInfo defined in the scope of the main module/page. 完成所有登录检查后,我基本上需要调用在主模块/页面范围内定义的setUserInfo函数。 I can close the modal by calling $scope.$parent.close() but I can't call setUserData because it appears to be in another scope.
我可以通过调用$ scope。$ parent.close()来关闭模式,但是我不能调用setUserData,因为它似乎在另一个作用域中。
Thanks a lot in advance for your help. 在此先感谢您的帮助。
Use rootScope for user data: 使用rootScope来存储用户数据:
app.controller('MainController', function($scope, $rootScope, $location) {
var UserId = null;
var Username = null;
$rootScope.setUserInfo = function(userId, username) {
UserId = userId;
Username = username;
};
}
(function() {
"use strict";
var login = angular.module("UserControllers", [ ]);
login.controller("LoginCtrl", function($http, $scope, $rootScope) {
/* Login checks and requests... */
$rootScope.setUserInfo(uid, un); //...
}
}();
var myApp = angular.module('example.services');
function example () {
Var user = this;
Student.setData = function(data) {
user.data = data;
}
}
myApp.service('example',[example]);
now include the service in your controller && modal controller : 现在将服务包含在控制器&&模态控制器中:
app.controller('MainController', function($scope, $rootScope, $location, example) {
//now call your set user data function to share data from modal to controller
var UserId = null;
var Username = null;
$rootScope.setUserInfo = function(userId, username) {
UserId = userId;
Username = username;
};
}
(function() {
"use strict";
var login = angular.module("UserControllers", [example ]);
login.controller("LoginCtrl", function($http, $scope, $rootScope, example) {
/* Login checks and requests... */
$rootScope.setUserInfo(uid, un); //...
}
}();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.