Controller component in AngularJS

An AngularJS application works primarily based on the controller component to control the flow of data in the application. A controller is defined using the directive as the ng-controller . A controller is a JavaScript object that includes attributes and functions. Each controller accepts $ scope as a parameter to navigate to the application / module that this controller part controls.

  ng-app = "" ng-controller = "sinhvienController" > . 

Here is the bioControllerController part that uses the ng-controller directive. In the next steps, we will define the contentController as follows:

 

sinhvienController means that a JavaScript object with $ scope is a parameter.

  1. $ scope is directed to the application and is used by the sinhvienController object.
  2. $ scope.sinhvien is the attribute of the sinhvienController object.

cough and ten are two properties of $ scope.sinhvien object. We create values ​​for them (ho: Quan Tri, ten: Mang).

HoTen is a function of $ scope.sinhvien object whose task is to return the value of the student's full name.

In the HoTen function, we get the student object and then return the matching name.

Note, we can also define controller objects in separate JS files and declare in the HTML page.

Now we use the propertyController attribute with ng-model or expression as follows:

 Enter last name : < input type = "text" ng - model = "sinhvien.ho" > 
Enter the name : < input type = "text" ng - model = "sinhvien.ten" >

You entered : {{ sinhvien . HoTen ()}}
  1. We have attached the value of sinhvien.ho and sinhvien.ten to the 2 input boxes.
  2. We have attached the value of sinhvien.HoTen () to the HTML page.
  3. Now, every time we enter a ho or ten value into the input box, this student's full name will be automatically updated.

For example

Below is an example for the explanation of the controller above: thanhphanController.html

 Ví dụ về Controller Angular JS  src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js" > 

QTM application

  ng-app = "mainApp" ng-controller = " sinhvien Controller" > Nhập tên:  type = "text" ng-model = " sinhvien .ten" >

Nhập họ: type = "text" ng-model = " sinhvien .ho" >

Bạn đã nhập: {{sinhvien.HoTen()}}

When running the above code, we get the following result:

Controller component in AngularJS Picture 1

Follow tutorialspoint

Previous post: Expression in AngularJS

Next article: HTML DOM element in AngularJS

5 ★ | 1 Vote

May be interested

  • View components in AngularJSView components in AngularJS
    angularjs supports single page application through multiple views on a single page. to do this, angularjs provides ng-view and ng-template directive and $ routeprovider service.
  • HTML DOM element in AngularJSHTML DOM element in AngularJS
    the directives below can be used to bind application data to attributes in the html dom element.
  • Create the first AngularJS applicationCreate the first AngularJS application
    to start, we start with creating the actual xinchao application by using angularjs, we will show you the specific parts of an angularjs application.
  • Multilingual (i18n) in AngularJSMultilingual (i18n) in AngularJS
    angularjs provides multi-lingual functions (i18n) with 3 types of filters, currency, date and number. we just need to combine the js corresponding to the location depending on the country. by default it will attach to the location in the web browser.
  • Expression in AngularJSExpression in AngularJS
    expression is used to bind application data to html tags. expression is written in {{expression}}. expression has a similar way of operating as the ng-bind directive.
  • Top 9 best MIDI Controller for musiciansTop 9 best MIDI Controller for musicians
    midi controller (midi controller) allows you to control virtual instruments with real keys or blank pad, making the experience more flexible. below is a list of the best midi controllers for you to choose from.
  • Create the table in AngularJSCreate the table in AngularJS
    table data is often repeated in natural order. in angularjs, ng-repeat directive can be used to draw tables easily. below is an example of how to use ng-repeat to create tables.
  • The best controllers for playing PC gamesThe best controllers for playing PC games
    here are the best pc controllers, selected based on criteria such as comfort, control and how they are compatible with pc games.
  • How to connect the PS4 controller to a computerHow to connect the PS4 controller to a computer
    how to connect the ps4 controller to a computer. ps4 controller (dualshock 4) gives you a more enjoyable and engaging gaming experience. however, connecting the ps4 controller to a computer is not as simple as you think, but must go through some of the steps presented in the following article.
  • How to connect a PS4 or Xbox One controller to a MacHow to connect a PS4 or Xbox One controller to a Mac
    if you don't want to use your mac keyboard and mouse to control your games, you can use an xbox or ps4 controller instead. connecting them to a macbook pro, imac or macbook air via bluetooth is actually quite simple.