AngularJS basic learning notes controller _ AngularJS

Source: Internet
Author: User
In AngularJS, the controller is a Javascript function (type class) used to enhance the scope instance except for the root scope unexpected. When you or AngularJS itself through & lt; code & gt; scope. $ new & lt; code & gt; when you create a new sub-scope object, there is an option for you to pass it as a parameter to the controller. AngularJS controller is used to control AngularJS applications.

AngularJS controller is a common JavaScript object.

AngularJS controller
AngularJS applications are controlled by controllers.

The ng-controller command defines an application controller.

A controller is a JavaScript Object, which can be created through a standard JavaScript Object constructor.

First Name:
Last Name:

Full Name: {{firstName + " " + lastName}}

《script》var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) { $scope.firstName = "John"; $scope.lastName = "Doe";});《script》

Code explanation:

AngularJS application is defined by ng-app = "myApp. The valid scope of this application is ng-app's

.

Ng-controller = "myCtrl" attribute is an AngularJS command, which defines a controller.

The myCtrl function is a common JavaScript function.

AngularJS uses the $ scope object to call the controller.

In AngularJS, $ scope is an application object (the owner of application variables and functions ).

The controller contains two attributes (or variables): firstName and lastName. They are appended to the $ scope object.

The ng-model command binds the value of the input tag to the attributes of the controller (firstName and lastName ).

Controller method
The preceding example shows that the controller object contains two attributes: lastName and firstName.

The controller can also contain methods (assign a function to a variable ):

First Name:
Last Name:

Full Name: {{fullName()}}

《script》var app = angular.module('myApp', []);app.controller('personCtrl', function($scope) { $scope.firstName = "John"; $scope.lastName = "Doe"; $scope.fullName = function() { return $scope.firstName + " " + $scope.lastName; }});《script》

Place the controller in an external file
In large applications, controller code is often written in external files.

Copy the code in the script label to the external file of personController. js:

First Name:
Last Name:

Full Name: {firstName + "" + lastName }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.