$setViewValue doesn't update ng-model

4,317 views
Skip to first unread message

Alex Figueiredo

unread,
Dec 14, 2012, 8:38:49 AM12/14/12
to ang...@googlegroups.com
Can someone tell me what I'm doing wrong here?

angular.module('customControl'[])
.directive('rangePicker'function ({
    
    return {
        restrict 'E',
        require '?ngModel',
        replace true,
        transclude true,
        template '<div class="range" ng-transclude></div>',
        
        link function (ngelemattrsngModel{
            
            var title 'Test';
            var 0;
            
            elem.bind('click'function ({
                console.log('click');
                ngModel.$setViewValue(title ++i);
            });
            
        }
    }
});


<div ng-app="customControl">
     <range-picker ng-model="modelo">
        Filtrar por período
     </range-picker>
     {{modelo}}
</div>

The problem is that my var "modelo" is never updated.

Peter Bacon Darwin

unread,
Dec 14, 2012, 8:54:05 AM12/14/12
to ang...@googlegroups.com
This handler: 

elem.bind('click'function ({
                console.log('click');
                ngModel.$setViewValue(title ++i);
            });
is being called from outside of AngularJS.  So you need to wrap it in a call to $apply().

elem.bind('click', ng.$apply(function ({

                console.log('click');
                ngModel.$setViewValue(title ++i);
            }));

Alex Figueiredo

unread,
Dec 14, 2012, 9:56:46 AM12/14/12
to ang...@googlegroups.com

Peter Bacon Darwin

unread,
Dec 14, 2012, 10:18:17 AM12/14/12
to ang...@googlegroups.com
Sorry, I typed too fast, what I gave you was calling $apply in the link function.
What you actually needed was to pass a new function that will $apply when the button is clicked.


Pete


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

Alex Figueiredo

unread,
Dec 14, 2012, 10:23:04 AM12/14/12
to ang...@googlegroups.com
Thank you very much! 

Dave Merrill

unread,
Dec 14, 2012, 10:23:33 AM12/14/12
to ang...@googlegroups.com
In your fiddle I see this error when I click run: "$apply already in progress". I'm too new to advise beyond that, but I doubt this can work as-is.

Dave 

Marjan Jangholi

unread,
Oct 29, 2019, 5:33:17 AM10/29/19
to Angular and AngularJS discussion
u can use ngModel.$render() after ngModel.$setViewValue(title ++i);
Reply all
Reply to author
Forward
0 new messages