Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Bootstrap Modal: Unknown provider: $modalInstanceProvider

I am trying to use the Angular Bootstrap Modal directive (http://angular-ui.github.io/bootstrap/) as follows, in my controller to open the modal:

function customerSearch() {     var modalInstance = $modal.open({         templateUrl: 'app/customer/customers.modal.html',         controller: 'customers.modal'     });      modalInstance.result.then(function(selectedCustomer) {         console.log(selectedCustomer);     }); } 

In the modal controller:

var controllerId = 'customers.modal';  angular.module('app').controller(controllerId,     ['$modalInstance', customersModal]);  function customersModal($modalInstance) {     // Modal controller stuff } 

But when I do, I get the following error:

Unknown provider: $modalInstanceProvider <- $modalInstance 

If I take out $modalInstance, it works but I obviously have no reference to the modal in the calling controller..

Edit

I don't know if it is worth noting, but I am using the Controller As syntax:

<div class="container-fluid" data-ng-controller="customers.modal as vm"> 

Application dependencies:

var app = angular.module('app', [     // Angular modules      'ngAnimate',        // animations     'ngRoute',          // routing     'ngSanitize',       // sanitizes html bindings (ex: sidebar.js)      // Custom modules      'common',           // common functions, logger, spinner     'common.bootstrap', // bootstrap dialog wrapper functions      // 3rd Party Modules     'ui.bootstrap',      // ui-bootstrap (ex: carousel, pagination, dialog)     'breeze.directives', // breeze validation directive (zValidate) ]); 

I've created a plunker which is showing the problem here: http://plnkr.co/edit/u8MSSegOnUQgsA36SMhg?p=preview

like image 487
CallumVass Avatar asked Jan 09 '14 12:01

CallumVass


People also ask

How to use ngbootstrap 6 modal in angular?

Angular 9|8 NgBootstrap 6 | Modal Tutorial By Example 1 #Update Angular CLI. Angular CLI tool needs to be updated for creating an Angular project with the latest stable version. 2 #Create New Angular Project. ... 3 #Install and Configure the ng-bootstrap package. ... 4 #Adding Bootstrap Styling 5 #Import Bootstrap Module. ...

What is a modal in angular?

This Angular post is compatible with Angular 4 upto latest versions, Angular 7, Angular 8, Angular 9, Angular 10, Angular 11, Angular 12 and Angular 13 Modals are dialogue box/ popup UI containers that are used to display content on top of others. It floats in the center of the screen which can be closed after actions. 1) What is ng-bootstrap?

How do I fix the unknown provider error in angular?

Since neither of these are declared, you will receive the "Unknown provider" error. To fix this, you should change all of the dependencies to strings: angular('myModule').service('myService', ['anotherService', function(anotherService) { // ...

What is the difference between modaldismissreasons and ngbmodaloptions?

ModalDismissReasons: It is used to identify the method used to close the modal ie. using ESC key or clicking on Babkdrop area. NgbModalOptions is used to configure Modal which can have the following properties: backdrop: It is the shadow created on Modal back, default is true, if set to 'static' then Modal doesn’t close on clicking outside.


1 Answers

The problem was that you were specifying a controller in 2 places - when opening a modal and inside a template - this is not needed. Remove ng-controller from a template and things will work as expected: http://plnkr.co/edit/khySg1gJjqz1Qv4g4cS5?p=preview

like image 113
pkozlowski.opensource Avatar answered Sep 25 '22 07:09

pkozlowski.opensource