如何转移$ scope.variables到控制器? [英] How to transfer $scope.variables to controller?
问题描述
我建立了一个系统,我有一个清单,列出变量项
- 标题
- 技能
- 预算
- 发布
我也有,当我在列表中的项目点击打开一个模式。我想借当前选定的项目,并在我的第二个模式的控制器使用其变量?这可能吗?
我在思维模式的使用,其中i缓存从数据库中抽取的所有数据服务的,但后来我不知道我怎么会只显示选定的项目,而不是每一个项目。
下面是我的控制器列表项:
app.controller('openProjectsCtrl',['$范围,$ HTTP,projectsModal',
功能($范围,$ HTTP,projectsModal){ $ http.get(HTTP://localhost/app/controllers/php/getProjects.php)
.success(功能(响应){
$ scope.projects = response.projects;
}); $ scope.showModal =功能(){
projectsModal.deactivate();
projectsModal.activate();
};}]);
模态控制器(我想要查看选定变量):
app.controller('projectsModalCtrl',['$范围,$超时','projectsModal',
功能($范围,$超时,projectsModal){VAR CTRL =这一点;ctrl.closeMe =功能(){
projectsModal.deactivate();
};}]);
是的,你可以使用一个控制器的变量使用两种方法的另一个控制器里面
- 创建服务之间的通信。
- 使用$ rootScope。$广播
样code
angular.module('为MyService,[])。服务('msgBus',函数(){
this.serviceValue = {}; }]);
});
和使用它的控制器是这样的:
控制器1
angular.module('为MyService,[])。控制器(CTRL1',函数($范围,msgBus){
$ scope.sendmsg =功能(){
msgBus.serviceValue =你好;
}
});
控制器2
angular.module('为MyService,[])。控制器('CTRL2',函数($范围,msgBus){
$ scope.checkValue(){
警报(msgBus.serviceValue);
}
});
I am building a system where i have items listed in a list with variables
- Title
- Skills
- Budget
- Posted
I also have a Modal that opens up when i click on an item in the list. I would like to take the current selected item and use its variables in my second modal controller? Is this possible?
I was thinking of using a service where i cache all data that is pulled from database, but then i dont know how i would only display selected item and not EVERY item in the modal.
Here is my controller for the list items:
app.controller('openProjectsCtrl', ['$scope', '$http', 'projectsModal',
function ($scope, $http, projectsModal) {
$http.get("http://localhost/app/controllers/php/getProjects.php")
.success(function (response) {
$scope.projects = response.projects;
});
$scope.showModal = function() {
projectsModal.deactivate();
projectsModal.activate();
};
}]);
Modal Controller(Where i want to view the Selected Variables):
app.controller('projectsModalCtrl', ['$scope', '$timeout', 'projectsModal',
function ($scope, $timeout, projectsModal) {
var ctrl = this;
ctrl.closeMe = function () {
projectsModal.deactivate();
};
}]);
Yes you can use variables of one controller inside another controller using two methods
- Create Service to communicate between them.
- Use $rootScope.$broadcast
sample code
angular.module('myservice', []).service('msgBus', function() {
this.serviceValue= {};
}]);
});
and use it in controller like this:
controller 1
angular.module('myservice', []).controller('ctrl1',function($scope, msgBus) {
$scope.sendmsg = function() {
msgBus.serviceValue='Hello';
}
});
controller 2
angular.module('myservice', []).controller('ctrl2',function($scope, msgBus) {
$scope.checkValue(){
alert( msgBus.serviceValue);
}
});
这篇关于如何转移$ scope.variables到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!