ejemplo clase 4 - Watch Simple

by scyrizales

HTML

<div ng-app="" ng-controller="Ctrl1">
    <input type="text" ng-model="prop" />
    <div>
        {{prueba}}
    </div>
    <select ng-model="sel">
        <option ng-repeat="i in items" value="{{ i.valor }}">
            {{ i.texto }}
        </option>
    </select>
    {{sel}}
    <br>
    <input type="text" ng-model="obj.p" />
</div>

JavaScript

function Ctrl1($scope){
    $scope.prop = "Algo";
    $scope.prueba = "Eso";
    $scope.items = [
        {valor: 1, texto: "a"},
        {valor: 2, texto: "b"},
        {valor: 3, texto: "c"}
    ];
    $scope.$watch("sel", 
        function(nV, oV){
            if (nV === "2") {
                $scope.sel = oV;
            }
        });
    $scope.obj = { p: "1" };
    $scope.$watch("obj.p", 
        function(newV, oldV){
        $scope.prueba = newV;
    });
}