Home>Article>Web Front-end> How to use AngularJS controller? Angularjs controller controls angularjs application instance resolution

How to use AngularJS controller? Angularjs controller controls angularjs application instance resolution

Robert De Niro
Robert De Niro Original
2018-09-08 16:46:53 1240browse

This article mainly introduces the use ofangularjscontrollers.angularjs controllerControl the data of angularjs application. Now let’s take a look at this article

Introduction to angularjs controller:

AngularJS controllerControlData of AngularJS application .

AngularJS controllers are regularJavaScript objects.

AngularJS Controller

AngularJS applications are controlled by controllers. The

ng-controllerdirective defines the application controller.

Controllers areJavaScript objectscreated by the standard JavaScriptobject constructor.

名:
名:

姓名: {{firstName + " " + lastName}}

Application resolution:

AngularJS applications are defined byng-app. The application runs within

.

ng-controller="myCtrl"attribute is an AngularJS directive.is used to define a controller.

myCtrlfunction is a JavaScript function.

AngularJS uses$scopeobjects to call controllers.

In AngularJS, $scope is an application object (belonging to application variables and functions).

The controller's$scope(equivalent to scope, control scope) is used to save the object of the AngularJS Model (model).

The controller creates two properties in the scope (firstNameandlastName).

ng-modelThe directive binds the input fields to the controller's properties (firstName and lastName). (If you want to see more, go to the PHP Chinese websiteAngularJS Development Manualcolumn to learn)

Controller method

The above example demonstrates a method with lastName and firstName. Controller object with two properties.

Controllers can also have methods (variables and functions):

名:
姓:

姓名: {{fullName()}}

Controllers in external files

In large applications, the controller is usually stored in an external file.

Just copy the code in the