javascript - Calling AngularJS function from a remote page loaded by Bootstrap modal -
i use bootstrap 3 modal component load remote form, in define angular controller , several functions. browser said "tried load angular more once".
main page:
(omitted: ng-app="manageappcollections", ng-controller="manageappcontroller") <button type="button" class="btn btn-success" ng-href="./appconfform" data-toggle="modal" data-target="#savemodal">add 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>
form page(./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>
the addtype()
function cannot triggered.
do need compile remote content? , how can that?
edit
previously loaded angularjs files in both pages, not necessary. remove files in form page, content in page won't work. assume needs compiling after loaded, now:
main page:
(omitted: ng-app="manageappcollections", ng-controller="manageappcontroller") <button type="button" class="btn btn-success" ng-href="./appconfform" data-toggle="modal" data-target="#savemodal">add 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'); }); } }
}])
but error becomes: argument 'saveformcontroller' not function, got undefined
you don't need create 2 'ng-app'.
with 2 controllers same ng-app, can job.
can try : (edit: code bellow doesn't works)
<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 becomes controller of manageappcollections app
edit
to work angularjs , bootrap, use angular-ui, it's easier : http://angular-ui.github.io/bootstrap/#/modal
edit2
i edited previous code, can check result here :
Comments
Post a Comment