0

Forgive me if my question is too basic. I am learning AngularJS's controllers.

This is the project structure.

AngularJS project

This is my html:

<!doctype html>
<html ng-app>

<head>
    <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
    <script src="js/controllers.js" />
</head>

<body>

<div ng-controller="numberController">
<input type = "text" ng-model = "number.number1" placeholder = "Enter a number here">

<input type = "text" ng-model = "number.number2" placeholder = "Enter another number here">
{{number.number1}} + {{number.number2}} = {{number.sumIt()}}

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

This is my controller:

function numberController($scope) {
    $scope.number = {
        number1: 0,
        number2: 0,

        sumIt: function() {
            var numberObject;
            numberObject = $scope.number;
            return parseInt(numberObject.number1) + parseInt(numberObject.number2);
        }
    };
}

The web page doesn't even load.

Where have I gone wrong?

Edit:

The error that I get: I get this error: https://docs.angularjs.org/error/$injector/modulerr?p0=sa-app&p1=Error:%20%5B$injector:nomod%5D%20http:%2F%2Ferrors.angularjs.org%2F1.3.3%2F$injector%2Fnomod%3Fp0%3Dsa-app%0A%20%20%20%20at%20Error%20(native)%0A%20%20%20%20at%20https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:6:416%0A%20%20%20%20at%20https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:21:366%0A%20%20%20%20at%20a%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:21:7)%0A%20%20%20%20at%20https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:21:250%0A%20%20%20%20at%20https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:35:105%0A%20%20%20%20at%20r%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:7:302)%0A%20%20%20%20at%20g%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:34:456)%0A%20%20%20%20at%20Lb%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:38:184)%0A%20%20%20%20at%20d%20(https:%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.3%2Fangular.min.js:17:381

My updated controller:

var saApp = angular.module("saApp", []);

saApp.controller('numberController', function($scope) {
    $scope.number = {
        number1: 0,
        number2: 0,
        sumIt: function() {
            var numberObject;
            numberObject = $scope.number;
            return parseInt(numberObject.number1) +      parseInt(numberObject.number2);
        }
    };
});

My updated HTML:

<!doctype html>
<html>

<head>
    <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
    <script src="js/controllers.js" />
</head>

<body>

<div ng-app = "saApp" ng-controller="numberController">
<input type = "text" ng-model = "number.number1" placeholder = "Enter a number here">

<input type = "text" ng-model = "number.number2" placeholder = "Enter another number here">
{{number.number1}} + {{number.number2}} = {{number.sumIt()}}

</div>
</body>
</html>
6
  • 2
    What's in your console? Commented Feb 25, 2016 at 11:11
  • 1
    you need the ng-app... the controller has to be part of an ng-app Commented Feb 25, 2016 at 11:12
  • 1
    There isn't a console. I just have a web browser. Commented Feb 25, 2016 at 11:12
  • 1
    Your web browser has a console... Commented Feb 25, 2016 at 11:13
  • I have added the error from the console. Commented Feb 25, 2016 at 11:18

9 Answers 9

3

You need to link your application with your JS file first. Change your tag to And change your controllers.js

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.number = {
        number1: 0,
        number2: 0,
        sumIt: function() {
            var numberObject;
            numberObject = $scope.number;
            return parseInt(numberObject.number1) +      parseInt(numberObject.number2);
        }
    };
});
</script>

Also the console is found if you right click on your mouse then click on Inspect element.

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

2 Comments

I have updated it. (See my updated question.) But it still doesn't work.
Try clearing your cache or just run it in Incognito mode. If it still does not work maybe it need a server try running it with XAMP/WAMP etc
1

Hi Write your program like this:-

<!doctype html>
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<script>
 angular.module("myapp", [])
    .controller("NumberController", function($scope, $http) {
       $scope.number = {
        number1: 0,
        number2: 0,
        sum:0,
        sumIt: function() {
            var numberObject;
            numberObject = $scope.number;
            this.sum = parseInt(numberObject.number1) + parseInt(numberObject.number2);
        }
    };
    } );
</script>
</head>
<body ng-app="myapp"> 
<div ng-controller="NumberController">
<input type = "text" ng-model = "number.number1" placeholder = "Enter a number here"/>

<input type = "text" ng-model = "number.number2" placeholder = "Enter another number here"/>
<input type = "button" ng-click="number.sumIt()" value="Sum"/>
sum is {{number.sum}}

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

Comments

1

The problem: you are using a deprecated method of controller declaration. As of at least version 1.3 you can't declare a controller with function <controller>.

The solution:

1: declare the app – var app = angular.module('myApp', []);

2: attach the controller

app.controller('numberController', ['$scope', function($scope) {

$scope.number = {
        number1: 0,
        number2: 0,

        sumIt: function() {
            var numberObject;
            numberObject = $scope.number;
            return parseInt(numberObject.number1) + parseInt(numberObject.number2);
        }
    };
}]);

I've made a jsfiddle to demonstrate.

Comments

0

You have to define angular.module

<html ng-app="myApp">

Then write your controller like below

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

myApp.controller('numberController', ['$scope', function($scope) {
   //Your controller code here
}]);

You can check working codepen Here

Useful link here
1 . Angular docs
2 . TutorialsPoint angular js

2 Comments

you can also check the link
I can get it to work if I move the script to the script tags inside the html file. I have no idea why this is the case.
0

With your code is not clear that you define the app module. From angular doc

// app.js
    (function(angular) {
        angular.module('ngAppDemo', []).controller('NumberController', function($scope){
            $scope.number = {
                number1: 0,
                number2: 0,

                sumIt: function() {
                    var numberObject;
                    numberObject = $scope.number;
                    return parseInt(numberObject.number1) + parseInt(numberObject.number2);
                }
            };
        });
    })(window.angular);

Notice that you are not using the naming convention for angular controllers, the controller name starts with upper case and the js function starts with lower case ( function numberController() - ng-controller="NumberController")

Finally insert ng-app on html or body, and controller on div

<body ng-app="ngAppDemo">
    <div ng-controller="numberController">

Comments

0

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

myApp.controller('numberqcontroller',  function($scope) {
   //$scope.number ={};
  $scope.number = {
        number1: 10,
        number2: 0,

        sumIt: function() {
            var numberObject;
            numberObject = $scope.number;
            return parseInt(numberObject.number1) + parseInt(numberObject.number2);
        }
    };
  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!doctype html>
<html >

<head>
   
</head>

<body ng-app="myApp">

<div ng-controller="numberqcontroller">
<input type = "text" ng-model = "number.number1" placeholder = "Enter a number here">

<input type = "text" ng-model = "number.number2" placeholder = "Enter another number here">
{{number.number1}} + {{number.number2}} = {{number.sumIt()}}


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

Comments

0

I modified your code and it works with the same directory structure. Here's the js file :

var app = angular.module('myApp', []);
app.controller('numberController', function($scope) {

        $scope.number = {
        number1: 0,
        number2: 0,

        sumIt: function() {
            var numberObject;
            numberObject = $scope.number;
            return parseInt(numberObject.number1) + parseInt(numberObject.number2);
        }
    }; 

});

and the html :

<!DOCTYPE html>
<html ng-app="myApp">

   <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
   <script src="js/controllers.js" > </script>

<body>
    <div ng-controller="numberController">

       <input type = "text" ng-model = "number.number1" placeholder = "Enter a number here">

       <input type = "text" ng-model = "number.number2" placeholder = "Enter another number here">

        {{number.number1}} + {{number.number2}} = {{number.sumIt()}}

    </div>

</body>
</html>

Comments

0

How about this:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>

  <div ng-controller="numberController as ctrl">
    <input type="number" ng-model="ctrl.number1" placeholder="Enter a number here">
    <br>
    <input type="number" ng-model="ctrl.number2" placeholder = "Enter another number here">
    <br>
    <div ng-if="ctrl.number1 && ctrl.number2">
      {{ctrl.number1}} + {{ctrl.number2}} = {{ctrl.sum(ctrl.number1, ctrl.number2)}}
    </div>
  </div>

</body>
</html>

With Javascript:

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

myApp.controller('numberController', function() {
  var vm = this;
  vm.number1 = null;
  vm.number2 = null;
  vm.sum = function(num1, num2) {
    return num1 + num2;
  };
});

JS Bin example

It's advised to use the 'controller as' construction. you should also consider using a directive (or component if you can upgrade to AngularJs 1.5) instead of using ng-controller.

Comments

0

Just do this in your controller

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

app.controller('numberController', function($scope) {
    $scope.number = {
        number1 : 0,
        number2 : 0,
        sumIt : function() {
            //no need to copy $scope.number to another variable just simply use like this
            return parseInt($scope.number.number1) + parseInt($scope.number.number2);
        }
    }
});

And here is html code

<body  ng-app="myapp">
    <div ng-controller="numberController">
    <input type = "text" ng-model = "number.number1" placeholder = "Enter a number here"/>
    <br /><br />
    <input type = "text" ng-model = "number.number2" placeholder = "Enter another number here"/>
    <br /><br />
     <!-  {{number.number1}} + {{number.number2}} = {{number.sumIt()}}  no need to do this just simply call the sumIt function like below->
     Total = {{number.sumIt()}}
    </div>
</body>

The finally the output

Output

Comments

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.