Building on the basic framework, you can add a model, which is a function that creates a class in which you assign data to field names. Then you can create instances of this class in your controller, and assign data values to the fields.
For more basic usage, you can omit the model and supply a single value for each field in the controller. The $scope parameter on the controller controls data context for the model, and you can add an optional $locale parameter.
Here is an example of a basic Person model with four data fields, and a controller with a list of four rows of data.
Basic Model and Controller |
Copy Code |
---|---|
<script type="text/javascript"> //Person class function Person(data) { this.ID = data.ID; this.Company = data.Company; this.Name = data.Name; this.Sales = data.Sales; }; function MyController($scope, $locale) { $scope.list = [ new Person({ ID: "ANATR", Company: "Ana Trujillo Emparedados y helados", Name: "Ana Trujillo", Sales: 8900 }), new Person({ ID: "ANTON", Company: "Antonio Moreno Taqueria", Name: "Antonio Moreno", Sales: 4500 }), new Person({ ID: "AROUT", Company: "Around the Horn", Name: "Thomas Hardy", Sales: 7600 }), new Person({ ID: "BERGS", Company: "Berglunds snabbkop", Name: "Christina Berglund", Sales: 3200 }) ]; } </script> |
Here is an example of a very simple controller without a model.
Basic Controller |
Copy Code |
---|---|
<script type="text/javascript"> // AngularJS controller used for this sample function MyController($scope) { $scope.val = 50; $scope.min = 0; $scope.max = 100; } </script> |