Aug 3, 2015

Angular js Controllers

The controller defines the actual behavior of your app. It contains business logic for the view and connects the model to view with the help of $scope. A controller is associated with a HTML element with the ng-controller directive.


<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
  <meta charset="utf-8">
  <title>MVC pattern in AngulaJS</title>
 
  <script type="text/javascript">
 //defining module
 var app=angular.module("app",[]);
   
 //defining controller
 app.controller('Ctrl', function ($scope) {
 
 //defining book's authors model
 var bookAuthors=["Shailendra Chauhan","Saksham Chauhan"]

 //defining book viewmodel
 $scope.book = { id: 'ABXY01', name: 'C# Interview Questions and Answers',
 authors: bookAuthors}
 });
</script>
</head>
<body ng-app="app">
<div ng-controller="Ctrl">
  <table>
   <tr>
      <td>Book Id :</td><td><span ng-bind="book.id"></span></td>
   </tr>
   <tr>
      <td>Name :</td><td><span ng-bind="book.name"></span></td>
   </tr>
   <tr>
      <td>Authors :</td><td> <span ng-bind="book.authors"></span></td>
   </tr>
 </table
</div>
</body>
</html>



Book Id :ABXY01
Name :C# Interview Questions and Answers
Authors : Shailendra Chauhan,Saksham Chauhan

No comments:

Post a Comment