在AngularJS中使用另一个控制器中的$scope函数
在AngularJS中使用另一个控制器中的$scope函数
这个问题在这里已经有答案了:
我想在另一个控制器中分享一个控制器的$scope函数,特别地,在这个例子中是为了一个AngularUI对话框。
具体来说,在下面的例子中,我想让$scope.scopeVar在PopupCtrl中可用。
main.js
angular.module('MyApp', ['ui.bootstrap']); var MainCtrl = ['$scope', '$dialog', '$rootScope', function($scope, $dialog, $rootScope) { $scope.myTestVar = "hello"; $scope.myOpts = { backdrop: true, keyboard: true, backdropClick: true, resolve: { MainCtrl: function() { return MainCtrl; }}, templateUrl: 'myPopup.html', controller: 'PopupCtrl' }; $scope.scopeVar = 'scope var string that should appear in both index.html and myPopup.html.'; $rootScope.rootScopeVar = "rootScope var string that should appear in both index.html and myPopup.html."; $scope.openDialog = function() { var d = $dialog.dialog($scope.myOpts); d.open().then(function() { $scope.scopeVar = 'scope var string should be changed after closing the popup the first time.'; $rootScope.rootScopeVar = 'rootScope var string should be changed after closing the popup the first time.'; }); }; }]; var PopupCtrl = ['$scope', 'dialog', 'MainCtrl', function ($scope, dialog, MainCtrl) { var key; for (key in MainCtrl) { $scope[key] = MainCtrl[key]; } $scope.close = function(){ dialog.close(); } }];
index.html
{{scopeVar}}
{{rootScopeVar}}
myPopup.html
{{scopeVar}}
{{rootScopeVar}}
admin 更改状态以发布 2023年5月23日