D
D
DieZz2015-09-23 12:51:45
Angular
DieZz, 2015-09-23 12:51:45

Why doesn't the $watch in the directive work?

Good day.
I am writing a directive, the essence of which is to call a modal window and send data to the server from a form in this window. I show the window using the Modal directive from Bootstrap components for Angular ( angular-ui.github.io/bootstrap ).
'templates/modal/addCom.html'

<select class="form-control" ng-model="selected">
    <option ng-repeat="type in types" value="{{type}}">{{type.comment}}</option>
</select>

app.directive('addCom',function($modal,addComService){
    return {
        restrict: 'A',
        link : function($scope, $element, $attributes){
            $element.on('click',function(){
                modal();
            });

            var modal = function(){
                var modalInstance = $modal.open({
                    animation: $scope.animationsEnabled,
                    templateUrl: 'templates/modal/addCom.html',
                    controller: function($scope,$modalInstance){

                        $scope.selected = {};
                        $scope.disabled = true;

                        $scope.$watch(
                            function(){
                                return $scope.selected;
                            },
                            function(newVal,oldVal){
                                //срабатывает только при вызове окна 
                                console.log(newVal);
                                //срабатывает только при вызове окна 
                                console.log(oldVal);
                            },
                            true
                        );

                    }
                })
            };
        }
    }
});

$watch only fires when the modal is opened. When the model changes, the selected $watch does not work, although if you display the value of the model in the view, you can see that the data is changing.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-09-23
@DieZz

scalars in scopes are not inherited, as they are passed by value. Variants, store data in an object, they are passed by reference and references are inherited.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question