1

I am trying to call a external js file to implement angular js controller. Please find the code below. But it is not giving the result for the external js file access code is being added. Could someone correct me what am i doing mistake here?

[It gives the proper result if I don't have added the external js access code]

<!DOCTYPE html>
<html >
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
    <body ng-app="myApp">

        <!-- Angular JS controller sample -->
        <div ng-controller="myAppCtrl">
            First name : <input type="text" ng-model="firstName"> <br>
            Last name : <input type="text" ng-model="lastName"> <br>
            Full name is : {{firstName + " " + lastName}} <br>
        </div>
        <script>
            var app = angular.module('myApp',[]);
            app.controller('myAppCtrl', function($scope) {
                $scope.firstName = "Prabakar"
                $scope.lastName = "Prabu"
            });
        </script>

        <!-- Method based controller -->
        <div ng-controller="methodCtrler"> 
            <br>
            First name is : <input type="text" ng-model="fName"> <br>
            Last name is : <input type="text" ng-model="lName"> <br>
            Full name : {{myFunction() }}
            <br>        
        </div>
        <script>
            //var app1 = angular.module('myApp',[]);
            app.controller ('methodCtrler', function($scope) {
                $scope.fName = "Jonathan"
                $scope.lName = "Gladwin" 
                $scope.myFunction = function() {
                    return $scope.fName + " " + $scope.lName;
                }
            });
        </script>

        <!-- Method call from external JS files -->
        <br>
        <div ng-controller ="ryanContrler">
            My first name : <input type="text" ng-model="ryanFirstname"> <br>
            My last name : <input type="text" ng-model="austinLastname"> <br>
            My full name : {{fullName()}
        </div>
        <script src ="ryanNameContorller.js"></script>

    </body>

</html>

ryanNameContorller.js

angular.module ('myApp', []).controller('ryanContrler', function($scope) {
    $scope.ryanFirstname = "Ryan",
    $scope.austinLastname = "Austin",
    $scope.fullName = function () {
        return $scope.ryanFirstname + " " + $scope.austinLastname; 
    }

});

And the result is,

enter image description here

1
  • Angular app didn't bootstrapped at all. Can you check the browser console and check the errors. Seems like you don't have ryanNameContorller.js file at the same place where your HTML file is. Commented Jul 31, 2015 at 5:19

3 Answers 3

1

You missed } in ryanContrler replace My full name : {{fullName()} with My full name : {{fullName()}}

Use this new code in ryanNameContorller.js file

angular.module('myApp').controller('ryanContrler', ['$scope', function($scope){
    $scope.ryanFirstname = "Ryan",
    $scope.austinLastname = "Austin",
    $scope.fullName = function() {
        return $scope.ryanFirstname + " " + $scope.austinLastname;
    }

}]);

HTML

<!DOCTYPE html>
<html >
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
    <body ng-app="myApp">

        <!-- Angular JS controller sample -->
        <div ng-controller="myAppCtrl">
            First name : <input type="text" ng-model="firstName"> <br>
            Last name : <input type="text" ng-model="lastName"> <br>
            Full name is : {{firstName + " " + lastName}} <br>
        </div>
        <script>
            var app = angular.module('myApp',[]);
            app.controller('myAppCtrl', function($scope) {
                $scope.firstName = "Prabakar"
                $scope.lastName = "Prabu"
            });
        </script>

        <!-- Method based controller -->
        <div ng-controller="methodCtrler"> 
            <br>
            First name is : <input type="text" ng-model="fName"> <br>
            Last name is : <input type="text" ng-model="lName"> <br>
            Full name : {{myFunction() }}
            <br>        
        </div>
        <script>
            //var app1 = angular.module('myApp',[]);
            app.controller ('methodCtrler', function($scope) {
                $scope.fName = "Jonathan"
                $scope.lName = "Gladwin" 
                $scope.myFunction = function() {
                    return $scope.fName + " " + $scope.lName;
                }
            });
        </script>

        <!-- Method call from external JS files -->
        <br>
        <div ng-controller ="ryanContrler">
            My first name : <input type="text" ng-model="ryanFirstname"> <br>
            My last name : <input type="text" ng-model="austinLastname"> <br>
            My full name : {{fullName()}}
        </div>
        <script src ="ryanNameContorller.js"></script>

    </body>

</html>

Demo here

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

6 Comments

Please check our discussion in "Shubham Nigam" post. we have done this already, still the last one where external JS file is being accessed is not giving the result.
Replace the ryanNameContorller.js code with what i provided and add the ryanNameContorller.js at the end.
@user1953977 i have added a demo in my updated answer.
Just one query. To run the code, when I right click in Eclipse->Open With Web Browser, it is not giving the expected result in eclipse web browser. But, When I open this in Chrome browser, I am getting the exact proper output. Why is that not working in eclipse web browser, any idea?
Sorry mate I don't have much knowledge on Eclipse. Better you can create a new question with this.
|
0

There are a couple mistakes in the code that might or might not contributed to the cause.

  1. You should spell Controller correctly. Ctrl/Ctrler/Contorller are just confusing yourself and whoever that needs to maintain your code.

  2. angular.module('myApp',[]) should only be called once. If you need to get the app in external js file, use angular.module('myApp').

  3. Put scripts inside <head> or <body>, don't put it in <html> directly.

  4. You should check console log first when you encounter error. If you don't understand it, post it in the question. (Hint: press F12 to open)

Comments

0

The problem is your <script> to add js file is after ng-controller <div> put it inside your <head> to make sure that it call first

Your HTML:->

<html >
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
 <script src ="ryanNameContorller.js"></script>
</head>
    <body ng-app="myApp">

        <!-- Angular JS controller sample -->
        <div ng-controller="myAppCtrl">
            First name : <input type="text" ng-model="firstName"> <br>
            Last name : <input type="text" ng-model="lastName"> <br>
            Full name is : {{firstName + " " + lastName}} <br>
        </div>
        <script>
            var app = angular.module('myApp',[]);
            app.controller('myAppCtrl', function($scope) {
                $scope.firstName = "Prabakar"
                $scope.lastName = "Prabu"
            });
        </script>

        <!-- Method based controller -->
        <div ng-controller="methodCtrler"> 
            <br>
            First name is : <input type="text" ng-model="fName"> <br>
            Last name is : <input type="text" ng-model="lName"> <br>
            Full name : {{myFunction() }}
            <br>        
        </div>
        <script>
            //var app1 = angular.module('myApp',[]);
            app.controller ('methodCtrler', function($scope) {
                $scope.fName = "Jonathan"
                $scope.lName = "Gladwin" 
                $scope.myFunction = function() {
                    return $scope.fName + " " + $scope.lName;
                }
            });
        </script>

        <!-- Method call from external JS files -->
        <br>
        <div ng-controller ="ryanContrler">
            My first name : <input type="text" ng-model="ryanFirstname"> <br>
            My last name : <input type="text" ng-model="austinLastname"> <br>
            My full name : {{fullName()}}
        </div>

    </body>

</html>

5 Comments

There was one close brach missing after fullName(), I added like this. Now, the first 2 controller is giving the correct result. The last one where I called external files doesn't give the expected result. Any problem in my ryanNameContorller.js ?
Have put it in <head> as i said??
then you need to post the code of ryanNameContorller.js
that was minnor mistake of '}' bracket in HTML
"Srinivas Pai" has given the answer. One query is, if i move <script src ="ryanNameContorller.js"></script> into <head>, it is not able to access this JS file. So, As per "Srinivas" I kept the script part, it is accessing well and giving the expected output.

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.