2

My directive contains an javascript object. I try to find with jquery all html elements which are binding to this javascript object. How can I get the binded data of the angularjs object with jquery?

Maybe the above code explains better my plan.

angular.module('app').directive('highlight', [
    '$rootScope', '$compile', function ($rootScope, $compile) {
        return {
            replace: true,
            templateUrl: 'highlight.html',
            link: function (scope, element, attrs) {

                //this data object do we need to highlight
                scope.Tohighlight;

                $($.find('input, select')).each(function () {
                    //todo extract data if input or selection are binding data
                    //<input type="x" data-ng-model="data" /></span>
                    var data = angular.extractDataFromhtmlelement($(this));
                    //if html element contains our data add some css stuff
                    if(data ==  scope.Tohighlight)
                    {

                    }
                });
            };
    }
]);

I don't know if there exists an angularjs method for my purpose. How can I get the binded angularjs data model of an jquery object? Thanks.

1

1 Answer 1

1

You may do it using scope() method and $parse service. Example

link: function(scope, element) {
    var inputElement = element.find('input');
    var inputModelGetter = $parse(inputElement.attr('ng-model'));
    var inputModelSetter = inputModelGetter.assign;
    function getInputModel() {
        return inputModelGetter(inputElement.scope());
    }
    function setInputModel(value) {
        inputModelSetter(inputElement.scope(), value);
    }
    // ...
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. But with element.find().scope() I do get the viewmodel of the element not the binding object/property.
@Briefkasten, I have updated my answer with $parse service. Think, it is what you want.
Thanks for your example. That's exactly what I need. My current problem is that the $parse(boundExpression); expression throws an "TypeError: object is not a function". When I debug the $parse object is available. Do you got any hint? My modify example jsfiddle.net/dizel3d/h5Ls3dbv
@Briefkasten, seems you gave wrong URL to an example.
Correct URL jsfiddle.net/28y2rps4 . When the user clicks on the template the setFocus function will be executed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.