Ng-Controller-4

 

Another example of Ng-Controller, We have created two methods in the controller and calling them on ng-click and that way the selection will be displayed in P tag. See the example

 
 
 
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome to LearnKode - A code learning platform</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> </head> <body ng-app="app"> <div ng-controller="controller"> <div class="container"> <br /> <button class="btn btn-default" ng-click="chiliSpicy()">Chili</button> <button class="btn btn-primary" ng-click="notChiliSpicy()">Not Chili</button><br /> <p>The food is <b>{{spice}}</b> spicy!</p> </div> </div> </body> </html> <script> var app = angular.module('app', []); app.controller('controller', function ($scope) { $scope.chiliSpicy = function () { return $scope.spice = "Chili"; }; $scope.notChiliSpicy = function () { return $scope.spice = "Not Chili"; }; }); </script>
Output