angularjsbootstrap-4bootstrap-modalangularjs-injector

angularJS and Bootstrap: Error: [$injector:unpr]


I am new to angularJS, also in web development, so here is my problem: I tried to create a book store webpage, where the user utilizes a modal to input data, the problem is when, trying to create a controller for the modal, for the cancel button, it give me the error that you see in the title, that is:

"angular.js:14961 Error: [$injector:unpr]".

I search a lot, but I don't find any solution. I can open the modal.

note: I am not a native English speaker, I tried in the Spanish page, without luck. I hope you can help me.

The code:

my index.html

<!DOCTYPE>
<html >
    <head>
        <meta charset="UTF-8">
        <title>Book Store</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    </head>
    <body ng-app="app" ng-controller="myController as ctrl">
        <h1>Welcome to book store</h1>
        <table >
            <tr>
                <th>Titulo</th>
                <th>Autor</th>
                <th>Editorial</th>
                <th>Edicion</th>
            </tr>
            <tr ng-repeat="x in listaLibros">
                <td>{{x.name}}</td>
                <td>{{x.autor}}</td>
                <td>{{x.editorial}}</td>
                <td>{{x.edition}}</td>
            </tr>
        </table>
        <div class="form-group">
            <button type="button" uib-tooltip="Click this button to do something amazing." class="btn btn-primary">Hover to show Tooltip</button>
        </div>


        <button type="button" class="btn btn-default" ng-click="control.open()">Open me!</button>

    </body>


    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/1.3.3/ui-bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/1.3.3/ui-bootstrap-tpls.min.js"></script>
    <script src= "app.js" ></script>
</html>

the agregarLibro.html

<html>
  <body></body>
<div class="modal-content">

    <div class="modal-header" ng-app="app">
        <h4 class="modal-title">Agregar libro</h4>


        <div class="modal-body">
        <h6>Titulo</h6>
        <input type="text" ng-model="listaLibros.name">
        <h6>Autor</h6>
        <input type="text" ng-model="listaLibros.autor">
        <h6>Editorial</h6>
        <input type="text" ng-model="listaLibros.editorial">
        <h6>Edicion</h6>
        <input type="text" ng-model="listaLibros.edition">
    </div>


    <button type="button" class="btn btn-danger" data-dismiss="modal" ng-click="$ctrl.cancel()">X</button>
    <button type="button" class="btn btn-success">Agregar libro</button>

</div>
</body>
</html>

and the app.js

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




app.controller("myController",function($scope, $http, $uibModal,){
    var control = this;

    $http.get("dataLibros.json")
    .then(function(response){
        $scope.listaLibros = response.data;
    });


    control.open = function(){
        var modalInstance = $uibModal.open({
            templateUrl: "agregarLibro.html",
            controller: 'ModalInstanceCtrl',
            controllerAs: '$ctrl',

        })
    }
});

app.controller('ModalInstanceCtrl', function ($uibModalInstance, items) {
    var $ctrl = this;

    $ctrl.cancel = function () {
      $uibModalInstance.dismiss('cancel');
    };
});

Solution

  • There are few mistakes,

    (i) You need to embed the scripts within the body tag, not oustside the body tag

      <div class="form-group">
        <button type="button" uib-tooltip="Click this button to do something amazing." class="btn btn-primary">Hover to show Tooltip</button>
      </div>
      <button type="button" class="btn btn-default" ng-click="control.open()">Open me!</button>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/1.3.3/ui-bootstrap.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/1.3.3/ui-bootstrap-tpls.min.js"></script>
      <script src="app.js"></script>
    </body>
    

    (ii) You should use `ng-click="ctrl.open()"

      <button type="button" class="btn btn-default" ng-click="ctrl.open()">Open me!</button>
    

    (iIi)Remove ng-app from your modal, there can be only one ng-app in an application

    <div class="modal-header" ng-app="app">
    

    DEMO