Javascript 为什么';你有权访问外部控制器吗?
假设我的主html中有:Javascript 为什么';你有权访问外部控制器吗?,javascript,angularjs,angular-ui-bootstrap,Javascript,Angularjs,Angular Ui Bootstrap,假设我的主html中有: <body ng-app="app" ng-controller="session as vmSession"> ... <!-- Modal will get created here --> </body> ... 在我的模式中,我有一个链接: <a ui-sref="siteManagement({ site: vmSession.user.site._id })" ng-click="$dismiss()">
<body ng-app="app" ng-controller="session as vmSession">
...
<!-- Modal will get created here -->
</body>
...
在我的模式中,我有一个链接:
<a ui-sref="siteManagement({ site: vmSession.user.site._id })" ng-click="$dismiss()">Example</a>
示例
该按钮没有正确的链接,而是指向根siteManagement
页面,而不是登录用户的特定页面。
我注意到模态窗口是用一个
ng隔离作用域
类创建的,这是阻止它工作的原因吗?您可以将任何变量传递给模态控制器:
$scope.openModal = function () {
var modalInstance = $modal.open({
templateUrl: '/templates/modal/yourModal.html',
controller: YourModalCtrl,
resolve: {
values: function () {
return {
aValue: '123',
anotherValue: {a: 'A', b: 'B'}
};
}
}
});
modalInstance.result.then(function (result) {
// ok result
}, function (result) {
// cancel result
});
};
var StepModalCtrl = function ($scope, $modalInstance, values) {
var aValue = values.aValue;
var anotherValue = values.anotherValue;
$scope.doSomething = function () {
console.log(aValue);
};
$scope.save = function () {
$modalInstance.close($scope.YourResult);
};
$scope.close = function () {
$modalInstance.dismiss($scope.YourResult);
};
};
您可以将任何变量传递给模态控制器:
$scope.openModal = function () {
var modalInstance = $modal.open({
templateUrl: '/templates/modal/yourModal.html',
controller: YourModalCtrl,
resolve: {
values: function () {
return {
aValue: '123',
anotherValue: {a: 'A', b: 'B'}
};
}
}
});
modalInstance.result.then(function (result) {
// ok result
}, function (result) {
// cancel result
});
};
var StepModalCtrl = function ($scope, $modalInstance, values) {
var aValue = values.aValue;
var anotherValue = values.anotherValue;
$scope.doSomething = function () {
console.log(aValue);
};
$scope.save = function () {
$modalInstance.close($scope.YourResult);
};
$scope.close = function () {
$modalInstance.dismiss($scope.YourResult);
};
};
使用
$modal.open()
时,可以指定模态控制器的作用域将从哪个$scope
对象继承,如下所示:
$modal.open({
templateUrl: 'modal.html',
controller: 'modal',
scope: $scope
});
如果缺少
作用域:
,它将默认为$rootScope
,这是会话控制器作用域的父级,这就是为什么它无法访问所需数据的原因。使用$modal.open()时
您可以指定模态控制器的作用域将从哪个$scope
对象继承,如下所示:
$modal.open({
templateUrl: 'modal.html',
controller: 'modal',
scope: $scope
});
如果缺少
作用域:
,它将默认为$rootScope
,它是会话控制器作用域的父级,这就是它无法访问您想要的数据的原因。如果您不想使用其中一个答案中建议的解析
选项,打开
方法有一个范围
选项。使用scope
选项,您可以传递希望modal的作用域基于的任何作用域。因此,您可以做的是将控制器的作用域作为scope
属性传递,并以模式“查看”控制器正在查看的所有变量
请注意,
$modal
将创建您提供的作用域的子作用域(如果未提供,$rootScope),以避免污染提供的作用域。如果您不想使用其中一个答案中建议的解析
选项,打开
方法有一个作用域
选项。使用scope
选项,您可以传递希望modal的作用域基于的任何作用域。因此,您可以做的是将控制器的作用域作为scope
属性传递,并以模式“查看”控制器正在查看的所有变量
请注意,
$modal
将创建您提供的作用域的子作用域(或$rootScope,如果没有提供),以避免污染所提供的作用域。我知道我可以这样做,但连接到根标记的会话控制器的整个要点是,我可以在整个应用程序中访问vmSession控制器,避免我在所有控制器中复制代码。为一个模态创建一个控制器只是为了给它传递一个应该已经可用的值似乎是很多不必要的工作。我知道我可以做到这一点,但是会话控制器被附加到根标记的整个要点是,我可以在整个应用程序中访问vmSession控制器,避免我在所有控制器中复制代码。为一个模态创建一个控制器只是为了给它传递一个应该已经可用的值,这似乎是很多不必要的工作。