AngularJS Tutorial - ng-click to call a function in scope








The following code shows how to use ng-click to call a function in scope.

Example


<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
<!--from   w w  w.j ava2s .  co  m-->
function Ctrl1($scope) {
    $scope.test = function(callback){
        eval(callback);
    };
}
</script>
</head>
<body>
  <div ng-app="">
  <div ng-controller="Ctrl1">
      <button ng-click="test('console.log(\'pushed\');')">Push Me</button>
  </div>
</div>
</body>
</html>

The code above is rendered as follows: