javascript - 范围绑定(bind)在模态弹出式 angularjs 中不起作用

标签 javascript jquery angularjs html twitter-bootstrap

我正在使用 Angular 将数据绑定(bind)到我的 UI,效果非常好。但是,当单击按钮时调用模态弹出窗口时,模态中的绑定(bind)不起作用。

enter image description here

<div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">{{checkItem}}</h4>
            </div>
            <div class="modal-body">

            </div>
            <div class="modal-footer">
                <button ng-click="saveClient()" class="btn btn-primary pull-right btn-tabkey"><i class="fa fa-save"></i>Save</button>&nbsp;&nbsp;
                <button type="button" class="btn btn-default" data-dismiss="modal" ng-click="focusInput=false"><i class="fa fa-ban"></i>Cancel</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>

Angular :

angular.module('myModule').controller('myController', ["$rootScope", "$scope", "$filter", "dataService", function ($rootScope, $scope, $filter, dataService) {

    $scope.checkItem = "";

    $scope.loadEditForm = function () {
        $scope.checkItem = "yes";
        $("#modal-form-edit").modal();
    };


}]);

最佳答案

似乎您正在使用纯 jQuery 方法打开模式。这在 Angular 中不起作用,因为打开的模式没有连接到 Angular 应用程序,所以它不知道必须处理模式、解析 HTML 等。

相反,您应该正确使用指令,或者在模态对话框的情况下,您可以简单地使用现有指令,例如 Angular UI 项目,它为 Angular 带来了现成的 Bootstrap 指令。在您的情况下,您需要 $modal服务。

那么用法就很简单了:

// remember to add ui.bootstrap module dependency
angular.module('myModule', ['ui.bootstrap']); 

angular.module('myModule').controller('myController', ["$rootScope", "$scope", "$filter", "$modal", "dataService", function ($rootScope, $scope, $filter, $modal, dataService) {

    $scope.checkItem = "";

    $scope.loadEditForm = function () {
        $scope.checkItem = "yes";
        $modal.open({
            templateUrl: 'modal.html',
            controller: 'modalController',
            scope: $scope
        });
    };

}]);

演示: http://plnkr.co/edit/kQz0fiaXLv7T37N8fzJU?p=preview

关于javascript - 范围绑定(bind)在模态弹出式 angularjs 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29161946/

相关文章:

jquery - Chrome 中的 Bootstrap 模式问题

javascript - 在 d3.js 生成的新元素中获取 Angular 以执行 ng-if

angularjs - Angular UI 模式指令,背景缺失

javascript - 如何在没有表单的情况下发布数据

jquery - 如果一个链接在 href 中包含某些内容,请将 href 更改为此

javascript - 如何向图像添加 .click() 事件?

javascript - AngularJS 中的指令、过滤器和异步调用

javascript - 如何从 JavaScript 获取数据到 Controller 并将其存储在数据库中

php - MySQL 查询在 AJAX 请求中花费大量时间

php - 当您回答问题并且有人提交了另一个答案时,stackoverflow 上显示的弹出警报是如何创建的?