Introduction
The introduction discusses the "controller as" syntax introduced in AngularJS, highlighting its purpose to reduce the use of $scope in controllers and improve code readability.
Benefits and Motivation
The answer explains the advantages of using this syntax:
Syntax and Example
The answer demonstrates the "controller as" syntax in the view and the controller. In the view, properties are accessed using the controller alias. In the controller, properties are assigned to the $scope object, which is then exposed through the controller alias.
Example:
HTML View | Controller |
---|---|
controller('InvoiceController', function() { this.qty = ...; }) |
Benefits Explained
Additional Insights
The answer provides further insights into the rationale behind the "controller as" syntax:
Code Examples
The answer includes links to Plunkr examples that demonstrate both the traditional $scope syntax and the "controller as" syntax.
The above is the detailed content of Why Should You Use \'Controller as\' Syntax in AngularJS?. For more information, please follow other related articles on the PHP Chinese website!