0

I am trying to use a controller that I wrote and saved in a controller.js file, but it won't work in my html code. I have the code written as: (This is the version that doesn't work correctly)

<div ng-app="myApp" ng-controller="movieCtrl">
   ..bit of my html code..
</div>
<script src="controller.js"></script>

This version however works just fine:

<div ng-app"myApp" ng-controller="movieCtrl">
      irrelavent html code here
</div>
<script>

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

app.controller('movieCtrl', function($scope, $http) {
    $scope.$watch('search', function() {
        fetch();
    });

    $scope.search = "tt2975590";

    function fetch() {
        $http.get("http:www.omdbapi.com/?i=" + $scope.search + "&plot=full&r=json")
            .then(function(response) {
                $scope.details = response.data;
            });
    }

    $scope.update = function(movie) {
        $scope.search = movie.Title;
    }
});
</script>

How do I get it to work with the controller in its own controller.js file?

23
  • Please, provide the controller code. Commented May 7, 2016 at 3:26
  • And... Maybe your src attribute has the wrong address. Commented May 7, 2016 at 3:27
  • How would I fix that? And there shouldn't be anything wrong with my controller code, as the code works perfectly when in the index.html file, but not when I try to put it in its own controller.js file and load it into the html code with <script src="controller.js"></script> Commented May 7, 2016 at 3:41
  • In what folder is in it? Commented May 7, 2016 at 3:42
  • I am only working on the controller, so its a very small and simple folder set up. I have a main project folder, and then the index.html file is in there, with another folder in there called controllers for the controller.js file Commented May 7, 2016 at 3:45

3 Answers 3

1

It isn't working because you have a syntax error here ng=controller="movieCtrl".

It should be ng-controller="movieCtrl"

I am assuming the rest of your code is correct. If that is right, then this change should fix your problems.

Sign up to request clarification or add additional context in comments.

12 Comments

that was just a typo when typing it up here, i do actually have it as ng-controller in my code
Then please provide more code because there is nothing wrong with what you have posted. What have you named the angular module when you declared it? Are you sure the script is being loaded? Also make sure the name of your controller is correct.
var app = angular.module('myApp', []); app.controller('movieCtrl', function($scope, $http) { $scope.$watch('search', function() { fetch(); }); $scope.search = "tt2975590"; function fetch() { $http.get("http:www.omdbapi.com/?i=" + $scope.search + "&plot=full&r=json") .then(function(response) { $scope.details = response.data; }); } });
I think the path to your controller.js file is wrong. But I am not for sure. What is the file structure of your project?
That is my code for the controller, which works perfectly fine if I put it directly into the html code inside the <script></script> but doesn't work if it has its own file named controller.js and is loaded into it in the code <script src="controller.js"></script>
|
0

So, as discussed, the problem was the relative path. Solution: Change the src attribute to "../app/controllers/controller.js".

1 Comment

Thank you very much for the help, I appreciate it.
0

You didn't instantiate your controller properly in your HTML code.

Here is a much cleaner solution that works. Remember to change ng=controller to ng-controller in your html code.

HTML Markup

<div ng-app="myApp" ng-controller="movieCtrl">
     //bit of html and Angular code such as...
     <input type="text" ng-model="watchInput" Placeholder="Type Something"/>
     <p>{{watchInput}}</p> 
</div>
<script src="controller.js"></script>

Controller in controller.js

Your AngularJS Controller should look something like...

myApp = angular.module ("myApp", []);
myApp.controller("movieCtrl", ["$scope", function($scope){
  //Your Controller code goes here
}]);

Hope this helps. Please let me know.

1 Comment

That was just a typo when typing it here, I do have it correct in my code as ng-controller. What you wrote looks almost identical to the code I wrote for the controller.js file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.