ANGULARJS bidirectional binding, manual implementation observation

Source: Internet
Author: User

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

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.