1

I am trying with angularjs but I get this error : Uncaught Error: [$injector:modulerr]

This is my html code :

<!DOCTYPE html>
<html ng-app="adphorusWork">
<head lang="en">
    <meta charset="UTF-8">
    <title>Search Movie</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="scripts/angular.min.js"></script>
    <script src="scripts/angular-route.min.js"></script>
    <script src="scripts/jquery-1.9.1.min.js"></script>
    <script src="app/search-movie.js"></script>
</head>
<body ng-controller="searchMovieController">
    <div class="navbar navbar-default navbar-fixed-top" style="margin-top: 20px;">
        <div class="container-fluid">
            <ul>
                <li>
                    <a href="http://www.omdbapi.com/">OMDb Api</a>
                </li>
            </ul>
        </div>
    </div>
    <div class="container" style="margin-top: 50px;">
        <div class="bs-docs-section" id="">
            <div class="row">
                <div class="col-lg-12">
                    <div class="page-header">
                        <h2><a href="https://github.com/Cakmakk">My Github</a></h2>
                    </div>
                    <div class="bs-component">
                        <form class="well form-search" id="search-movie" onsubmit="return false;">
                            <fieldset>
                                <legend>Search Movie</legend>
                            </fieldset>
                            <div>
                                <label class="control-label" for="movie-name">Movie Name : </label>
                                <input type="text" id="movie-name" class="input-small" style="margin-left: 10px;">
                                <button id="btn-search-movie" type="button" class="btn-sm btn-primary" style="margin-left: 10px;" ng-click="searchMovie()">
                                    <span class="glyphicon glyphicon-search"> Search</span>
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

and this is my search-movie.js code as the following :

(function () {
    'use strict';
    angular.module('adphorusWork', ['ngRoute'])
       .controller('searchMovieController',
       ['$scope','$route',
       function ($scope,$route) {
           $scope.searchMovie = function () {
               alert("come")
           }
       }]);
})();

When I'am working I get an error like in the picture enter image description here

also for example When I'am searching 'adphorusWork' I can see this : Search finished.No matched found. It should not be like this. I should see angular.module('adphorusWork', ['ngRoute']) in search-movie.js

Where am I doing wrong ?

eagle
  • 451
  • 1
  • 7
  • 27

2 Answers2

0

Make sure you've added angular and angular-route file.

see pluker : https://plnkr.co/edit/xZFrNepOa5wsbM0lMUbV?p=preview

it is working fine for me without an error.

(function (angular) {
'use strict';
angular.module('adphorusWork', ['ngRoute'])
   .controller('searchMovieController',
   ['$scope','$route',
   function ($scope,$route) {
       $scope.searchMovie = function () {
           alert("come")
       }
   }]);
})(angular);
Varit J Patel
  • 3,497
  • 1
  • 13
  • 21
  • 1
    also, a good practise when using the global closure is to pass the objects you're working with inside the closure in the parameters. In your case, pass `angular` to the global closure. – ValLeNain Jul 07 '16 at 11:46
  • I changed this -> old : search-movie.js new : app.js and worked. Thanks. – eagle Jul 07 '16 at 11:51
  • 2
    you'll have to define it in the function signature too :) `(function(angular){/*your code*/})(angular);` – ValLeNain Jul 07 '16 at 11:57
0

include the following:

var app = angular.module("myApp", ["ngRoute"]);

kgui
  • 4,015
  • 5
  • 41
  • 53