Search code examples
javascriptangularjsbuttonangularjs-ng-click

Angular.js output value live on ng-click


I'm new to angular and little bit confused with it. So basically I created a simple button and i want to run function foo() whitch assigns variable var one = 1; to $scope and outputs it in <p>{{one}}<p> every time its clicked like in live typing but this seems not working. Please provide me a solution to this.

<html ng-app="app">
 <!-- Body tag augmented with ngController directive  -->
 <body ng-controller="myController">
   <input type="text" ng-model="name">
   <p>{{name}}</p>
   <p>{{one}}</p>

   <button ng-click="foo()">1</button>

   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
   <script src="controller.js"></script>
 </body>
</html>

controller:

var app = angular.module('app',[]);
app.controller('myController', ['$scope',function($scope){
  var one = 1;

  $scope.name = name;

  function foo(){
    return $scope.one = one;
  }
}]);

Solution

  • function foo() dont exist in $scope your controller "myController"

    if you declaration:

    $scope.foo = function(){}

    in your controller, then this work for you