【问题标题】:Returning data from modal body template in Angular modal service从 Angular 模态服务中的模态体模板返回数据
【发布时间】:2015-05-12 09:48:37
【问题描述】:

我正在使用Dan Wahlin's modal 服务并使其正常工作,但是如果我更改 modal.html 以在正文中包含部分模板,我不知道如何获取模型模态的“确定”点击结果中的模板数据。

herehere 有两个类似的问题,但它们在模态表单中包含模型数据,而不是在单独的模板中。

我的想法是我可以有一个用于结果数据的模板控制器,但是我将如何从模态服务访问它并将其返回给调用控制器?

modal.html:

<div class="inmodal">
<div class="modal-header">
    <h4 class="modal-title">{{modalOptions.headerText}}</h4>
</div>
<div class="modal-body">
    <div ng-include="modalOptions.bodyTemplateUrl" ></div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-white" data-ng-click="modalOptions.close()">{{modalOptions.closeButtonText}}</button>
    <button class="btn btn-primary" data-ng-click="modalOptions.ok();">{{modalOptions.actionButtonText}}</button>
</div>

控制器:

var modalOptions = {
            closeButtonText: 'Cancel',
            actionButtonText: 'Save',
            headerText: 'Modal Title',
            bodyText: 'Nicy body.',
            bodyTemplateUrl: 'modules/users/views/account/user-password.partial.html' 
        };
modalService.showModal({}, modalOptions).then(function (result) {
            console.log(result.currentPassword);
            if (result === 'ok') {
                $scope.changeUserPassword();
            }
        });

用户密码.partial.html:

<div > <!-- PasswordController? -->
<form name="passwordForm" class=" form-horizontal" autocomplete="off">
    <fieldset>
        <div class="form-group">
            <input type="password" id="currentPassword" name="currentPassword" class="form-control" data-ng-model="passwordDetails.currentPassword" placeholder="Current password">
        </div>
        <div class="form-group">
            <input type="password" id="newPassword" name="newPassword" class="form-control" data-ng-model="passwordDetails.newPassword" placeholder="New password">
        </div>
        <div class="form-group">
            <input type="password" id="verifyPassword" name="verifyPassword" class="form-control" data-ng-model="passwordDetails.verifyPassword" placeholder="Verify password">
        </div>
    </fieldset>
</form>

我没有创建 plunker,因为上面的链接显示了相同的代码,除了我包含的 bodyTemplateUrl。

【问题讨论】:

  • 你能发布你的部分内容吗?
  • 当然,我已经编辑了我的问题。

标签: javascript angularjs


【解决方案1】:

您可以使用 ng-include onload 函数来定义一个变量,该变量将在模态和包含的 html 中可用:

<div ng-include="modalOptions.bodyTemplateUrl" onload="passwordDetails = {}"></div>

然后发送到ok方法:

<button type="submit" class="btn btn-primary" data-ng-click="modalOptions.ok(passwordDetails)">{{modalOptions.actionButtonText}}</button>

查看this plunker

【讨论】:

  • 那是缺失的部分!感谢您的时间和详细的 plunker。
  • 这很有帮助。我所做的不是使用 ng-include,而是将内容直接构建到我的 modal.html 版本中。
【解决方案2】:

如果您查看docs,您会看到上面写着“此指令创建新范围。”。这意味着当您使用 ng-include passwordDetails 时,正在查看控制器的子范围。您可以通过在您的模型前面加上 $parent 来告诉您的子模型查看父范围。

类似$parent.modelName

【讨论】:

  • 好的,我现在了解 new scope 问题,但我仍然不确定如何将子范围模型数据获取到模态控制器的“ok”结果.有代码sn-p帮助我理解吗?
猜你喜欢
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多