Implement a requirement where the value of a text box is displayed somewhere in the page, and the result of each change in the text box value changes dynamically.
An object can be declared in a controller, and one of its fields is used to store the initial value:
$scope. Funding = {startingestimate:0};
Above, a funding object is declared, its startingestimate field is used to store the initial value, and the initial value here is 0.
A function should also be declared in the controller to assign the result computed from the initial value to another field of the funding object.
$scope. computeneeded = function () {
$scope. funding.needed = $scope. funding.startingestimate * 10;
};
Above, declares the computeneeded function, it assigns the result of the funding object Startingestimate value to calculate the value to the funding object's another field needed.
Well, the input on the page needs to be bound to the startingestimate of the funding object, with each value change in ng-model;input need to call computeneeded, with Ng-change:
Initial value: <input type= "text" ng-model= "funding.startingestimate" ng-change= "computeneeded ()" >
As for the display results, just go to the funding object's needed field:
Computed value: {{funding.needed}}
However, there is a small problem: now, the computeneeded () method is triggered only when the value in input changes, and we want to execute the Computedneeded method when the page loads.
For this reason, Angularjs prepared $scope for us. $watch (The object being observed, the action performed), the Startingestimate field of the funding object can be listed as the observed object, each change of the Startingestimate field, Performs the computedneeded () action, including the initial assignment of the Startingestimate field.
$scope. $watch (' Funding.startingestimate ', $scope. computeneeded);
The complete code is as follows:
<!doctype html>
myApp" >
<meta charset= "UTF-8" >
<title>untitled document</title>
<script src= "angular.min.js" ></script>
<script>
var myapp = Angular.module ("myapp", []);
Myapp.controller ("mycontroller", [' $scope ',function($scope) {
$scope. Funding = {startingestimate:0};
function (){
$scope. funding.needed = $scope. funding.startingestimate * 10;
};
$scope. $Watch(' Funding.startingestimate ', $scope. computeneeded);
function (){
Alert ("done");
};
}]);
</script>
<body ng-controller= "mycontroller" >
<form ng-controller= "mycontroller" ng-submit= "finish ()" >
Initial value: <input type= "text" ng-model= "funding.startingestimate" ng-change= "computeneeded () ">
Computed value: {{funding.needed}}
<input type= "Submit" value= " submitted
</form>
</body>
Summarize:
Two-way binding: Ng-model
text box value Change event: Ng-change
Observe an object: $scope. $watch (the action performed by the observed object)
Form submission: Ng-submit
Other events: Ng-click, Ng-dblclick
Reference:<< developing next-generation Web applications with Angularjs >>
ANGULARJS bidirectional binding, manual implementation observation