【问题标题】:Angular Modal Scope Value Not Setting角模态范围值未设置
【发布时间】:2015-06-03 23:29:39
【问题描述】:

我正在使用 Angular Bootstrap UI 和 Modal。我的主控制器中有一个在控制器负载时实例化的变量。

$scope.links = {
            imagesa: ""
        };

在我关闭模式后,此变量没有设置为值“哭泣”。

有什么想法吗?

这是打开模态对话框的功能

    $scope.instagramModal = function (size) {

        var modalInstance = $modal.open({
            templateUrl: 'instagramModal.html',
            controller: 'InstagramModalInstanceCtrl',
            size: size,
            resolve: {
                items: function () {
                    return $sails.get("/instagram/self").success(function (response) {

                        return response.data;

                    }).error(function (response) {
                        console.log('error');
                    });
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.links.imagesa = 'Wept';

        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };

这是 modalDialog 控制器

.controller('InstagramModalInstanceCtrl', function ($scope, $modalInstance,items) {

        $scope.instapics = JSON.parse(items.data);

        $scope.selectIG = function(index) {
         $modalInstance.close($scope.instapics.data[index]);
        }

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

【问题讨论】:

  • 您阅读过文档吗?它明确指出:“范围 - 用于模式内容的范围实例(实际上 $modal 服务将创建所提供范围的子范围)。默认为 $rootScope”。将您的范围传递给$modal.open,或使用$close(result)
  • $scope.links.imagesa = "wept" 未设置,这已设置 $scope.testthisset = "set"。我如何将值分配给对象有问题吗?我在主控制器范围内初始化了对象。

标签: angularjs angularjs-scope angular-ui-router angular-bootstrap


【解决方案1】:

你是如何关闭模态的?

您必须使用.close() 而不是dismiss() 关闭模式

$modalInstance.close();

【讨论】:

  • 从模态控制器返回值。当它在 modalInstance.result.then 函数中时,未设置全局范围内的主要变量。我更新了问题以显示模式代码
【解决方案2】:

您的items 对象返回的是$promise 对象,而不是您所期望的data 对象。

我不会解决这样的注入依赖。我可能会将对$sails 的调用打包到服务中。但是,如果您想这样做,请尝试在模态控制器中使用 success 函数。

resolve: {
    items: function () {
        return $sails.get("/instagram/self");
}

//在你的模态控制器上

.controller('InstagramModalInstanceCtrl', function ($scope, $modalInstance, items) {

    items.then(function (response) {
        $scope.instapics = JSON.parse(response.data);
    })
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2016-04-30
    • 2016-06-03
    相关资源
    最近更新 更多