formations/javascript/angular/scope-4.md

590 B

.cadoles-slide-title[L'objet $scope (4/4)]

Observer les modifications du $scope

<html>
  <body ng-app="myApp">

    <div ng-controller="MainCtrl">
      <input ng-model="message" />
    </div>

    <script src="angular.js"></script>
    <script>
      var myApp = angular.module('myApp', []);

      myApp.controller('MainCtrl', ['$scope', function($scope) {

        $scope.message = '';

        $scope.$watch('message', function(newVal, oldVal) {
          console.log('"message" a changé !', newVal, oldVal);
        });

      }]);

    </script>
  </body>
</html>