以$工作发出和$从子模态父angularjs [英] Working with $emit and $on from child modal to parent angularjs

查看:146
本文介绍了以$工作发出和$从子模态父angularjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况。

两个文件,​​无论是在相同的应用程序

two files, both in the same app

var app = angular.module('myapp');

文件一个是家长和我有:

file one is the parent and I have:

app.controller("ControllerOne", ['$scope', '$http', '$modal', 
function ($scope, $http, $modal) {


$scope.$on('refreshList', function (event, data) {
    console.log(data);
});

$scope.openModal = function () {

    var modalInstance = $modal.open({
        templateUrl: '/SomeFolder/FileWithControllerTwo',
        controller: 'ControllerTwo',
        size: 'lg',
        resolve: {
            someParam: function () {
                return "param"
            }
        }
    });
}

}]);

文件二是孩子,我有:

file two is the child and I have:

app.controller("ControllerTwo", ['$scope', '$http', 'someParam',
    function ($scope, $http, someParam) {


        $scope.SaveSomething = function () {

            $http.post(url, obj)
                .success(function (data) {

                        $scope.$emit('refreshList', [1,2,3]);

                }).error(function () {

                });

        };

    }]);

假设我可以打开模式,我可以SaveSomething。

Assuming that i can open the modal and I can "SaveSomething".

我需要做的,从ControllerTwo发送一些数据ControllerOne?

What I need to do to send some data from ControllerTwo to ControllerOne?

我已经选中这个帖子以$范围工作。$排放和。$在
但我cant't解决了这个问题。

I already checked this post Working with $scope.$emit and .$on but I cant't solve the problem yet.

观测值:


  • FileOne.js - >我有ControllerOne(parrent) - > $关于

  • FileTwo.js - >我有ControllerTwo(子) - > $ EMIT

  • 是的,我打$ http.post.success条件
  • 里面的code
  • FileOne.js -> I have the ControllerOne (parrent) -> $on
  • FileTwo.js -> I have the ControllerTwo (child) -> $emit
  • Yes, I can hit the code inside $http.post.success condition

推荐答案

假设你正在使用的角度,用户界面​​引导(其中有一个$模型),然后在模型中的$范围为$ rootScope的childscope。

Assuming you are using angular-ui bootstrap (which has a $model), then the $scope in the model is a childscope of $rootScope.

根据 $模型文档可以提供 ControllerOne $范围使用范围选项,这将使模式的 $范围无论你提供的孩子。因此:

According to $model documentation you can supply the ControllerOne $scope by using the scope option which will make the modal's $scope a child of whatever you supply. Thus:

var modalInstance = $modal.open({
    templateUrl: '/SomeFolder/FileWithControllerTwo',
    controller: 'ControllerTwo',
    size: 'lg',
    scope: $scope,
    resolve: {
        someParam: function () {
            return "param"
        }
    }
});

然后,你可以发出来,使用 $范围。$父。$​​放出(...)。严格地说,这将创建一个连接,它假定用户模态的监听事件。

Then you could emit to that using $scope.$parent.$emit(...). Strictly speaking, this creates a coupling in that it assumes that the user of the modal listens to the events.

如果您不希望以注入的范围,他们可以注入 $ rootScope 并放出这一点。不过,这也将发送事件到应用程序中的每一个范围。

If you don't want to inject your scope, they you could inject $rootScope and emit on that. But that would also send the event to every scope in the application.

这是假设你真的想离开模式打开,发送消息回父控制器。否则,只要使用的close()辞退()方法。

This is assuming that you actually want to leave the modal open and send a message back to the parent controller. Otherwise, just use close() or dismiss() methods.

这篇关于以$工作发出和$从子模态父angularjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆