Angular UI Bootstrap Modal not working with ng-include

Real-time example: http://plnkr.co/edit/wWS3UFB3IZ0cAI4u2x04?p=preview

When you click "Open Modal 1", the following error appears:

Error: The expected dialog template or urll template was not found. Use options or the open method to specify them.

However, Modal 2, which does not use ng-include , works fine.

Also, if ui-bootstrap-tpls-0.1.0.js included instead of ui-bootstrap-tpls-0.2.0.js , everything works fine.

Any ideas?

+4
source share
2 answers

I believe that this problem is associated with a change in the modal directive to the terminal one. This means that other directives (e.g. ng-include) will not be processed along with modal ones. Here is the commit that made this change:

https://github.com/angular-ui/bootstrap/commit/ec796ec2299d03ddfb821e97047c0329f11ab962#src/modal/modal.js

I honestly donโ€™t know enough about this to know exactly why this directive should be terminal, but one simple solution is to simply use ng-include as a child modal, and not as a second directive acting on the same element. Here's what I mean:

 <div modal="opened1"> <ng-include src="'modal1.html'"></ng-include> </div> 

Updated live example: http://plnkr.co/edit/niYVIL9l8L8niBz56S4z?p=preview

+10
source

I don't know which version of Angular -ui was, but the documentation for modal windows remains confusing. Although you can actually use the ngInclude directive as part of showing a modal window, this is optional, as shown below. In the case below, the modal html and script are in separate files, and modal.open () is used to reference and display them. For some reason, I was able to process the updated model only when it was passed as a property of the $ scope .. object (see "Vm." In the code)

Modalist .html

 <!-- html elements reference the ModalViewCtrl decorated $scope --> <input ng-model='vm.email' type='email' placeholder='Email address' autofocus /> <input ng-model="vm.password" type="password" class="form-control" placeholder="Password" /> <label class="checkbox"> <input ng-model="vm.rememberMe" type="checkbox" /> Remember me </label> 

Debugger ModalViewCtrl.js

  angular.module('app') .controller('ModalViewCtrl', function($scope, $modal, parms) { /* decorate the $scope and/or preset the $scope.vm object as needed with parms.parm1, parms.parm2, .... */ $scope.ok = function () { $modalInstance.close($scope.vm); }; $scope.cancel = function () { $modalInstance.dismiss(); }; }); 

SourcePageCtrl.js source parameter

  angular.module('app') .controller('SourcePageCtrl', function($scope, $modal, ...) { $scope.open = function() { var modalInstance = $modal.open({ templateUrl: 'path_literal_to_ModalView.html' ,controller: 'ModalViewCtrl' ,resolve : { parms : function() { return { parms1 : value1 ,parm2 : value2 ,... }} }); modalInstance.result.then( function (vm) { $scope.result = vm; } , function () { ... } ); }); }); 

If you want to use include, however, there are two errors.

  • "templateUrl" must reference the identifier of the modal elements containing the tag, such as the identifier of ngInclude itself, which is the opposite of the URL of the template file.
  • Since angular evaluates the value of the attribute, if the src attribute of the path to the element is provided to the literal, it must be enclosed in a quote inside the already specified line:

+2
source

Source: https://habr.com/ru/post/1468944/


All Articles