简体   繁体   English

如何从 ui-modal AngularJS 调用父 function?

[英]How can I call a parent function from ui-modal AngularJS?

Here is my modal function:这是我的模态 function:

$uibModal.open({
   templateUrl: config.baseUrl + '/ClientApp/Views/Modals/userEditModal.html' + config.scriptVersion,
   size: size,
   scope: $scope,
   controller: function ($scope, $uibModalInstance) {                
     $scope.cancel = function () {
       $uibModalInstance.close();
     };
   }
}).result.catch(function (resp) {           
   if (['cancel', 'backdrop click', 'escape key press'].indexOf(resp) === -1) {
     throw resp;
   }
})

This $uibModal is inside a controller where I have other $scope variables and functions.这个$uibModal在 controller 里面,我有其他$scope变量和函数。 If I do an ng-repeat inside the modal with a parent scope object the ng-repeat works.如果我使用父 scope object 在模态中执行ng-repeat -repeat,则ng-repeat有效。 But if I call a function ex.但是,如果我打电话给 function ex。 doSomething() or try to use a scope object in ng-class ex. doSomething()或尝试在ng-class ex 中使用 scope object。 ng-class="{'css': doSomething()}" - it doesn't. ng-class="{'css': doSomething()}" - 它没有。 As you can see I assigned $scope to the scope property in the modal.如您所见,我将$scope分配给了模态中的 scope 属性。 What am I doing wrong?我究竟做错了什么? I also tried calling the function with $parent and still doesn't work ex.我还尝试使用$parent调用 function 并且仍然无法正常工作。 ng-if="$parent.doSomething()"

What about just using a reference to the function that already exists when you define the modal?仅使用对定义模态时已经存在的 function 的引用怎么样? bind a property to your controller's scope and pass it around to your modal. 将属性绑定到控制器的 scope并将其传递给您的模态。

bindToController: true,
controllerAs: 'parent'

Then just pass doSomething然后就通过doSomething

function myController($uibModal) {

    var parent = this;
    parent.doSomething = function() {
        /** does something ... **/
    };

    $uibModal.open({
       templateUrl: config.baseUrl + '/ClientApp/Views/Modals/userEditModal.html' + config.scriptVersion,
       size: size,
       scope: $scope,
       controller: function ($scope, $uibModalInstance) {  

         $scope.doSomethingDelegate = parent.doSomething;

         // ... more stuff
       }
    })
}

And then in your modal template然后在你的模态模板中

<element ng-if="doSomethingDelegate()"></element>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM