从Bootstrap模式加载的远程页面调用AngularJS函数

时间:2015-06-15 07:11:38

标签: javascript angularjs twitter-bootstrap

我使用Bootstrap 3模态组件来加载远程表单,其中我定义了一个Angular控制器和几个函数。但是浏览器说“试图加载角度不止一次”。

主页:

(omitted: ng-app="manageAppCollections", ng-controller="manageAppController")

<button type="button" class="btn btn-success" ng-href="./appConfForm" data-toggle="modal" data-target="#saveModal">Add an App</button>

<div id="saveModal" class="modal inmodal fade" aria-hidden="true" role="dialog" tabindex="-1" refresh-modal>
    <div class="modal-dialog">
        <div class="modal-content">
        </div>
    </div>
</div>

表单页面(./ appConfForm):

<div ng-app="saveForm" ng-controller="saveFormController">
    <div class="modal-header">
        <button class="close" data-dismiss="modal" type="button">
            <span aria-hidden="true">×</span>
            <span class="sr-only">Close</span>
        </button>
    </div>
    <div class="modal-body">
        <form class="form-horizontal" name="eventEditForm">
            (omitted form content)
        </form>
    </div>
    <div class="modal-footer">
        <button class="btn btn-default" data-dismiss="modal" type="button">Cancel</button>
        <button class="btn btn-primary" type="button" ng-click='addApp()'>Submit</button>
    </div>
</div>
<script>
    angular.module('saveForm',[]).controller('saveFormController', ['$scope, $http', function($scope, $http) {
        $scope.addApp = function(){
            console.log("Added!");
        }
    }])
</script>

无法触发addType()功能。

我是否需要编译远程内容?我怎么能这样做?

修改

之前我在两个页面中都加载了AngularJS文件,这是不必要的。现在我删除表单页面中的那些文件,但页面中的内容将不起作用。我认为它需要在加载后进行编译,所以现在:

主页:

(omitted: ng-app="manageAppCollections", ng-controller="manageAppController")

<button type="button" class="btn btn-success" ng-href="./appConfForm" data-toggle="modal" data-target="#saveModal">Add an App</button>

<div id="saveModal" class="modal inmodal fade" aria-hidden="true" role="dialog" tabindex="-1" refresh-modal>
    <div class="modal-dialog">
        <div class="modal-content">
        </div>
    </div>
</div>

<script>
angular.module('manageAppCollections').directive("refreshModal", ['$compile', function($compile) {
return {
    restrict: 'A',
    link: function(scope, element, attrs) {
        element.on('loaded.bs.modal', function(e) {
            $compile(element.contents())(scope);
        }).on('hidden.bs.modal', function (e) {
            element.removeData('bs.modal');
        });
    }
}

}])     

但现在错误变为:Argument 'saveFormController' is not a function, got undefined

1 个答案:

答案 0 :(得分:1)

您无需创建两个“ng-app”。

只有两个控制器但具有相同的ng-app,您可以完成这项工作。

你可以尝试这样的事情:(编辑:下面的代码不起作用)

<div ng-controller="saveFormController">
    <div class="modal-header">
        <button class="close" data-dismiss="modal" type="button">
            <span aria-hidden="true">×</span>
            <span class="sr-only">Close</span>
        </button>
    </div>
    <div class="modal-body">
        <form class="form-horizontal" name="eventEditForm">
            (omitted form content)
        </form>
    </div>
    <div class="modal-footer">
        <button class="btn btn-default" data-dismiss="modal" type="button">Cancel</button>
        <button class="btn btn-primary" type="button" ng-click='addApp()'>Submit</button>
    </div>
</div>
angular.module('manageAppCollections').controller('saveFormController', ['$scope, $http', function($scope, $http) {
    $scope.addApp = function(){
      console.log("Added!");
    }
}])

saveFormController 成为 manageAppCollections 应用

的控制器

修改

要使用AngularJS和Bootrap,你也可以使用angular-ui,它更容易:http://angular-ui.github.io/bootstrap/#/modal

<强> EDIT2

我编辑了以前的代码,你可以在这里查看结果:

Plunkr

(来自文档)