AngularJS UI引导模式是无法从范围履行职能 [英] AngularJS UI Bootstrap modal is unable to perform functions from scope
问题描述
在我angularjs的应用程序我使用UI引导创建情态动词。我传递范围和定制控制器进入模式,它显示了从最初的范围我的数据,但不能执行任何其功能。
我有主控制器:
In my angularjs app I use UI Bootstrap for creating modals. I pass scope and custom controller into the modal, it shows my data from original scope but cannot perform any of its function. I have main controller:
myapp.controller('WsCtrl', function WsCtrl($scope, $location, todoStorage, filterFilter, $modal, $log) {
在控制器我旁边:
$scope.items = ['item1', 'item2', 'item3'];
$scope.open = function () {
var modalInstance = $modal.open({
templateUrl: 'partials/users.html',
scope: $scope,
controller: ModalInstanceCtrl,
resolve: {
items: function () {
return $scope.items;
},
users: function(){
return $scope.users;
},
CurrentDate: function(){
return $scope.CurrentDate;
}
}
});
modalInstance.result.then(function (selectedItem) {
console.log(selectedItem);
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
和我也有控制器之外的另一个功能:
And also I have another function outside the controller:
var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
$scope.items = items;
$scope.users = users;
$scope.CurrentDate = CurrentDate;
$scope.selected = {
item: $scope.items[0]
};
$scope.num = 11;
$scope.ok = function () {
$modalInstance.close($scope);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};
当我传递的范围模式 - 我可以看到我的所有用户,但我不能在我原来的范围内添加一个关闭导致问题的功能。
When I pass the scope to modal - I can see all my users, but I can't add one 'cause off problem with functions in my original scope.
推荐答案
您不需要范围:$范围
。在决心
参数是负责传递变量ModalInstanceCtrl。但是,必须将这些参数添加到它的依赖(他们的名字必须与从决心
),所以如果你有:
You don't need scope: $scope
. The resolve
parameter is responsible for passing variables to ModalInstanceCtrl. But you must add those parameters to its dependencies (their names must match those from resolve
), so if you had:
resolve: {
foo: function(){
return $scope.something
}
}
,那么你必须有
var ModalInstanceCtrl = function ($scope, $modalInstance, foo) {
$scope.foo = foo;
// ...
}
呵呵,和功能都可以通过就像其他的变量,在解析
:
resolve: {
someFunction: function(){
return $scope.someFunctionFromOriginalScope
}
}
这篇关于AngularJS UI引导模式是无法从范围履行职能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!