从角引导模态返回值 [英] Returning a value from a modal in Angular Bootstrap

查看:123
本文介绍了从角引导模态返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从使用角度引导模态指令,弹出一个对话框,更改值(即传递中),然后进行检索。

I'm trying to use the modal directive from Angular Bootstrap to pop up a dialog, change a value (that was passed in) and then retrieve it.

然而,由于某种原因,该值永远不会在范围更新。而且,事实上,如果我把一个NG变化,并坚持在这一个断点,似乎有出于某种原因正在创建的范围另一个层次。

However, for some reason the value never gets updated in the scope. And, in fact, if I put in a "ng-change" and stick a breakpoint in it, it seems that there's another level of scope being created for some reason.

我创建这里plunker:<一href=\"http://plnkr.co/edit/Vy6gLgOJbWcLsHJtaGpV?p=$p$pview\">http://plnkr.co/edit/Vy6gLgOJbWcLsHJtaGpV?p=$p$pview

I've created a plunker here: http://plnkr.co/edit/Vy6gLgOJbWcLsHJtaGpV?p=preview

我这个百思不得其解。任何想法?

I'm baffled by this. Any ideas?

推荐答案

的Javascript流逝值原语(如整数)。所以,当你从解析函数返回一个整数,或者接受它作为一个函数参数,您使用的原整数的副本。于是改变它(如你在弹出那样)将会对原来的没有影响。

Javascript passes primitives (such as integer) by value. So when you return an integer from the resolve function, or accept it as a function argument, you're using a copy of the original integer. So then changing it (as you do in the popup) will have no effect on the original.

一个解决方法是使用一个对象,并通过周围。例如而不是整数'小时',使用对象时间:

A solution to this is to use an object, and pass that around. e.g. instead of an integer 'hour', use an object time:

$scope.time = {
  hour: 12
}; 

,并确保你的决心对象使用它:

and make sure you use it in the resolve object:

resolve : {
  time : function() {
    return $scope.time;
  }
}

您可以在 http://plnkr.co/edit/8YQGTn79AO4X7Tb7ann7看到了吗? p = preVIEW

修改:从plnkr提取

    var testApp = angular.module('testApp', ['ui.bootstrap' ]);

    testApp.controller('UserDataCtrl',function ($scope, $modal) {
        $scope.time = {
          hour: 12
        };
        $scope.showPopup = function() {
            $modal.open({
                templateUrl : 'popup.html',
                controller : PopupCtrl,
                resolve : {
                    time : function() {
                        return $scope.time;
                    }
                }
            }).result.then(function(result) {
                $scope.hour = result;
            });
        };

    });

var PopupCtrl = function($scope, $modalInstance, $http, time) {
    $scope.level="PopupCtrl";

    $scope.possibleTimes= [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23];

    $scope.time = time;

    $scope.ok = function() {
        $modalInstance.close($scope.hour);  
    };

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

    $scope.changing = function(){
        var x = 5;
    };

};

这篇关于从角引导模态返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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