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.
- $ scope is directed to the application and is used by the sinhvienController object.
- $ 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 ()}}
- We have attached the value of sinhvien.ho and sinhvien.ten to the 2 input boxes.
- We have attached the value of sinhvien.HoTen () to the HTML page.
- 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
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:
Follow tutorialspoint
Previous post: Expression in AngularJS
Next article: HTML DOM element in AngularJS
You should read it
May be interested
- View components in AngularJSangularjs 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 AngularJSthe directives below can be used to bind application data to attributes in the html dom element.
- Create the first AngularJS applicationto 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 AngularJSangularjs 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 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 musiciansmidi 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 AngularJStable 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 gameshere 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. 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 Macif 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.