Angular : Update model from directive

24,093

I think you are just missing the use of $apply. See it working here: http://jsfiddle.net/4TnkE/

element.bind('click', function() {
    scope.$apply(function() {
        scope.ngModel = !scope.ngModel;
    });
});

It can also be used like this to avoid nesting in another function:

element.bind('click', function() {
    scope.ngModel = !scope.ngModel;
    scope.$apply();
});
Share:
24,093
kreek
Author by

kreek

I have extensive experience designing and building scalable distributed web services. I’m currently a Staff Software Engineer with Ad Hoc LLC and the Department of Veterans Affairs. For the past three years I’ve helped turn the VA’s web offerings from a fractured experience into a one-stop shop. Allowing veterans to efficiently receive the benefits they’ve earned. I was one of the founding developers on vets.gov. A United States Digital Service project functioning as a start-up within the VA. Veteran outcomes improved dramatically via vets.gov. So much so that within two years of launch the VA secretary decided it should become the new va.gov. How we built the site received as much praise as the end-product. Agile methodologies, automated tests, CI/CD, cloud services (AWS), active monitoring, and user centered design were all foreign to the VA before vets.gov. Those practices are being propagated throughout the VA via my current project. The Veteran Service Platform, or VSP. VSP helps development teams succeed, by building on top of our APIs, and running our digital services playbook.

Updated on March 07, 2020

Comments

  • kreek
    kreek about 4 years

    I have a fiddle up here: http://jsfiddle.net/KdkKE/44/

    What I'd like to do create a 'toggle' component, basically a custom checkbox but with html that changes if it is true or false, which is bound to a boolean in a controller.

    When the user clicks on the toggle the model is updated the directive's view changes. It's similar to the examples at the end of the directives doc http://docs.angularjs.org/guide/directive but the state would be bound so that it would be correct on startup.

    var app = angular.module('App', []);
    
    function Ctrl($scope) {
        $scope.init = function() {
            $scope.foo = true
        }
    }
    
     app.directive('toggle', function() {
            return {
                restrict: 'E',
                replace: true,
                transclude: true,
                scope: {
                    label: '@',
                    ngModel: '='
                },
                template: 
                    '<div style="cursor: hand; cursor: pointer">{{label}}: {{ngModel}}</div>',
                link: function(scope, element, attrs, controller) {
                    element.bind('click', function() {
                        scope.ngModel = false;
                        attrs.$set('ngModel', false);
                        console.log('plz', attrs.ngModel);
                    });
                }
            };
        });
    

    -

    <div ng-app="App">
        <div ng-controller="Ctrl" ng-init="init()">
            <p>Foo in Ctrl: {{foo}}</p>  
            <toggle label="Foo" ng-model="foo"></toggle>
        </div>    
    </div>