AngularJS Custom Directive Two Way Binding

前端 未结 5 1070
甜味超标
甜味超标 2020-12-29 08:17

If I have an AngularJS directive without a template and I want it to set a property on the current scope, what is the best way to do it?

For example, a directive tha

5条回答
  •  甜味超标
    2020-12-29 09:05

    Change template to:

    
    

    Click Count: {{ counter.val }}

    and directive to:

    .directive('twoway',
        function() {
            return {
                scope: {
                    localValue: '=?bind'
                },
                link: function(scope, elem, attrs) {
                    scope.localValue = {
                        val: 0
                    };
                    elem.on('click', function() {
                        scope.localValue.val = scope.localValue.val + 1;
                        scope.$apply();
                    });
                }
            };
        }
    );
    

提交回复
热议问题