|
| 1 | +<!doctype html> |
| 2 | +<html ng-app="Demo"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + |
| 6 | + <title> |
| 7 | + Exploring One-Time Bindings In AngularJS 1.3 |
| 8 | + </title> |
| 9 | + |
| 10 | + <link rel="stylesheet" type="text/css" href="./demo.css"></link> |
| 11 | +</head> |
| 12 | +<body ng-controller="AppController"> |
| 13 | + |
| 14 | + <h1> |
| 15 | + Exploring One-Time Bindings In AngularJS 1.3 |
| 16 | + </h1> |
| 17 | + |
| 18 | + <p> |
| 19 | + <em>View the console.</em> |
| 20 | + </p> |
| 21 | + |
| 22 | + |
| 23 | + <!-- Load scripts. --> |
| 24 | + <script type="text/javascript" src="../../vendor/angularjs/angular-1.3.8.min.js"></script> |
| 25 | + <script type="text/javascript"> |
| 26 | + |
| 27 | + // Create an application module for our demo. |
| 28 | + var app = angular.module( "Demo", [] ); |
| 29 | + |
| 30 | + |
| 31 | + // -------------------------------------------------- // |
| 32 | + // -------------------------------------------------- // |
| 33 | + |
| 34 | + |
| 35 | + // I control the root of the application. |
| 36 | + app.controller( |
| 37 | + "AppController", |
| 38 | + function( $scope, $parse, $timeout ) { |
| 39 | + |
| 40 | + // The one-time binding feature isn't one thing, exactly. It's the |
| 41 | + // interplay between parsed expressions and $watch() bindings. An |
| 42 | + // expression will be flagged as a "one-time" expression if it starts |
| 43 | + // with "::". The "::" is not part of the expression - it gets stripped |
| 44 | + // out before AngularJS actually parses the expression. |
| 45 | + var accessor = $parse( "::( 'My Friend, ' + friend )" ); |
| 46 | + |
| 47 | + |
| 48 | + // Now that we have our parsed expression (as an accessor object), we can |
| 49 | + // test its functionality both when accessed directly (as a function) and |
| 50 | + // when consumed in a $watch() binding. |
| 51 | + $scope.friend = "Kim"; |
| 52 | + |
| 53 | + console.log( "From parse:", accessor( $scope ) ); |
| 54 | + |
| 55 | + $scope.$watch( |
| 56 | + accessor, |
| 57 | + function handleModelChange( newValue ) { |
| 58 | + |
| 59 | + console.log( "From watch:", newValue ); |
| 60 | + |
| 61 | + } |
| 62 | + ); |
| 63 | + |
| 64 | + // For the first watch binding, we're passing in the already-parsed |
| 65 | + // expression; but, you can use the one-time binding syntax directly in |
| 66 | + // the $watch() binding, as this value is simply passed to $parse() |
| 67 | + // service behind the scenes. |
| 68 | + $scope.$watch( |
| 69 | + "::friend", |
| 70 | + function handleModelChange( newValue ) { |
| 71 | + |
| 72 | + console.log( "From watch (2):", newValue ); |
| 73 | + |
| 74 | + } |
| 75 | + ); |
| 76 | + |
| 77 | + |
| 78 | + // Change the value at a later point in time (to make sure we're not in |
| 79 | + // the same digest as the Controller body). |
| 80 | + $timeout( |
| 81 | + function asyncProcessing() { |
| 82 | + |
| 83 | + // NOTE: This change in $scope.friend would normally trigger the |
| 84 | + // the $watch() binding above; but when you look at the console |
| 85 | + // output, you will see that the $watch() callback is only |
| 86 | + // invoked once, for the initial configuration (so long as it |
| 87 | + // doesn't result in an undefined value). |
| 88 | + $scope.friend = "Sarah"; |
| 89 | + |
| 90 | + console.log( "From parse:", accessor( $scope ) ); |
| 91 | + |
| 92 | + }, |
| 93 | + 10 |
| 94 | + ); |
| 95 | + |
| 96 | + } |
| 97 | + ); |
| 98 | + |
| 99 | + </script> |
| 100 | + |
| 101 | +</body> |
| 102 | +</html> |
0 commit comments