简体   繁体   English

AngularJS将模态控制器内部的表单传递给模态外部的ng-controller

[英]Angularjs Pass form which is inside modal controller to ng-controller outside modal

So I am a bit confused as to why this doesn't work: 因此,我对为什么不起作用感到困惑:

My main Angular page has an ng-controller called "SearchCtrl" which contains a bunch of logic for sending searches to a webserver. 我的主要Angular页面有一个名为“ SearchCtrl”的ng控制器,其中包含一堆用于将搜索发送到网络服务器的逻辑。

app.controller('SearchCtrl', ['$http','$scope', function($http,$scope) {

$scope.doAlert = function() {
    console.log('search page alert inside searchresultsctrl');
}

This controller is included on my search page,and then somewhere down the page there is another controller, SearchSortModalCtrl, which will pop open a modal and let users do some stuff: 这个控制器包含在我的搜索页面中,然后在页面的某个地方还有另一个控制器SearchSortModalCtrl,它将弹出一个模态并让用户做一些事情:

app.controller('SearchSortModalCtrl', function ($scope, $modal, $log) {
.....

var modalInstance = $modal.open({
        templateUrl: '/modal-search-sort.html',
        controller: 'SearchSortFormCtrl',
        size: size,
        resolve: {
            items: function () {
              return $scope.items;
            }
        }
    });

That is connected to this controller: 连接到该控制器:

app.controller('SearchSortFormCtrl', ['$http','$scope','$rootScope','$modalInstance', function($http,$scope,$rootScope,$modalInstance) {

    $scope.cancel = function () {
        $modalInstance.dismiss();
    };

    $scope.doAlert2 = function() {
        console.log('search page alert test2');
    }

So anyhow, what I am trying to do is to put an ng-submit inside the modal form, and when that form is submitted, it will do some logic on the SearchCtrl. 因此,无论如何,我想做的是将ng-submit放在模式形式内,提交该形式时,它将对SearchCtrl执行一些逻辑。

But if I do 但是如果我这样做

ng-submit="doAlert()"

inside my modal, it will not work or send anything to SearchCtrl, but if I change it to doAlert2() it will of course run on the SearchSortFormCtrl. 在我的模态中,它将不起作用或将任何内容发送到SearchCtrl,但是如果将其更改为doAlert2(),则它当然将在SearchSortFormCtrl上运行。 I tried making it 我试着做

ng-submit="$parent.doAlert()"

but no dice and to show you what level I'm flailing away on I even tried 但没有骰子,向您展示我什至尝试达到的水平

ng-submit="$parent.$parent.doAlert()"

and that did nothing. 那什么也没做。

What am I doing wrong, or how can I get this form to just do stuff in the SearchCtrl? 我做错了什么,或者如何获取此表单以仅在SearchCtrl中执行操作?

Since your controller is a child controller of SearchCtrl , any of its descendants (except for isolated scoped directives) will automatically have doAlert function inherited. 由于您的控制器是SearchCtrl的子控制器, SearchCtrl其任何后代(隔离的作用域指令除外)都将自动继承doAlert函数。 In your case you are not using scope option in modal settings so the modal scope will ultimately be $rootScope which angular ui modal does. 在您的情况下,您不在模态设置中使用scope选项,因此模态范围最终将是angular ui模态使用的$rootScope You can provide a scope option in the settings from the controller. 您可以在控制器的设置中提供范围选项。 So: 所以:

Either set scope:$scope or if you do not want to pollute current controller scope with properties set by modal controller set a childscope, ie scope:$scope.$new() . 设置scope:$scope或者如果您不想使用模态控制器设置的属性污染当前控制器范围,则设置一个子scope:$scope.$new() ,即scope:$scope.$new()

var modalInstance = $modal.open({
    templateUrl: '/modal-search-sort.html',
    controller: 'SearchSortFormCtrl',
    size: size,
    scope: $scope, //Or $scope.$new()
    resolve: {
        items: function () {
          return $scope.items;
        }
    }
});

Documentation link 文档链接

scope - a scope instance to be used for the modal's content (actually the $modal service is going to create a child scope of a provided scope). scope-用于模式内容的范围实例(实际上$ modal服务将创建所提供范围的子范围)。 Defaults to $rootScope 默认为$ rootScope

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

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