Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to open bootstrap modal window as a route

I'm using bootstrap to display a modal and want it to be shown on click of a anchor tag as a route. But i'm getting a module error & can't seem to figure out how to resolve it.

HTML

<div ng-view>
    <div ng-controller="DetailPageCtrl">
      <a href="#/profile">Click here to open modal!</a>
    </div>
    <script type="text/ng-template" id="modalContainer">
        <div ng-controller="ProfileModalCtrl"></div>
    </script>
</div>

JS

var app = angular.module('plunker', ['ui.bootstrap']);
app.config(function($routeProvider) {
   $routeProvider
      .when('/profile', {
          templateUrl : 'modalContainer',
          controller : 'ProfileModalCtrl'
      });
})
app.controller('DetailPageCtrl', function($scope) {
   console.log("detail page");
});
app.controller('ProfileModalCtrl', function($scope, $modal) {
    $modal.open({templateUrl : 'modal.html'});
});

Code in plnkr : http://plnkr.co/edit/VbvuWzLqkICFzBYI9sL5?p=preview

like image 444
user1184100 Avatar asked Dec 14 '13 16:12

user1184100


2 Answers

Demo is plagued with problems. You haven't included angular-route.js. You didn't provide a default path using otherwise and you placed html within ng-view

/* include script tag with `angular-route.js , then inject as dependency*/
var app = angular.module('plunker', ['ui.bootstrap', 'ngRoute']);
app.config(function($routeProvider) {
  $routeProvider.when('/', {
    templateUrl: 'default'
  })
    .when('/profile', {
      templateUrl: 'modalContainer',
      controller: 'ProfileModalCtrl'
    }).otherwise({
      redirectTo: '/'
    })
});
<div ng-view><!-- leave empty --></div>

DEMO

You will also run into problems declaring same ng-controller in markup as in route config...pick one or the other

like image 82
charlietfl Avatar answered Sep 22 '22 21:09

charlietfl


Your plunker is missing the ngRoute dependency. In newer versions of angular, ngRoute is a separate library that needs to included separately and declared as a module dependency to your app module:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.js"></script>
<script src="http://code.angularjs.org/1.2.4/angular-route.js"></script>
var app = angular.module('plunker', ['ngRoute', 'ui.bootstrap']);

Also, your routes are not fully defined.

Also, your templates (<script type="text/ng-template">) are defined inside the <div ng-view> element. ng-view is a directive which will replace the content of host div element when route is resolved, so a better place for your templates is outside of ng-view element.

Fixed PLUNKER

var app = angular.module('plunker', ['ngRoute', 'ui.bootstrap']);

app.config(function($routeProvider) {
 $routeProvider
  .when('/profile', {
    templateUrl : 'modalContainer',
    controller : 'ProfileModalCtrl'
  })
  .when('/detail', {
    templateUrl : 'detail.html',
    controller : 'DetailPageCtrl'
  })
  .otherwise({redirectTo: '/detail'});
});

app.controller('DetailPageCtrl', function($scope) {
 console.log("detail page");
});

app.controller('ProfileModalCtrl', function($scope, $modal) {
  $modal.open({templateUrl : 'modal.html'});
});
<!doctype html>
<html ng-app="plunker">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.js"></script>
    <script src="http://code.angularjs.org/1.2.4/angular-route.js"></script>

    <script src="http://angular-ui.github.com/bootstrap/ui-bootstrap-tpls-0.7.0.js"></script>

    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
  </head>
  <body>

    <div ng-controller="DetailPageCtrl">
      <a href="#/profile">Click here to open modal!</a>
    </div>

    <script type="text/ng-template" id="modalContainer">
        <div ng-controller="ProfileModalCtrl"></div>
    </script>

    <div ng-view></div>

</body>
<script src="script.js"></script>
</html>
like image 43
Stewie Avatar answered Sep 23 '22 21:09

Stewie