2

I am using angularJS and the $route service to load a partial view file like so.

<ng-view onload="onViewLoad()">
</ng-view>

works fine.

My Problem is trying to call a jQuery function on an html element that is returned specifically trying to call the jQuery bigtext() function on a header class from the partial file. I have tried

        $scope.onViewLoad = function(data){
            $(.bigTitle).bigtext({
                maxfontsize: 40 // default is 528 (in px)
            };
        });

and many similar things, but my problem is that I can not figure out how to select an element by class or id from the partial view. Can anyone tell me or lead me in the right direction to what the reason I can not access elements from the partial view. If a more robust sample of any of the code will help let me know. thanks

1 Answer 1

1

Anything like this needs to be done in a directive. This assures you the element exists when code fires and is simple to call the plugin directly on the element ... no need to worry about classes or ID's

HTML

<h3 big-text>My Header</h3>

Directive

angular.module('myApp').directive('bigText', function () {
    return {
        restrict: 'A', // "A" for attribute
        link: function (scope, elem, attrs) {
            // elem is jQ object
            elem.bigtext({
                maxfontsize: 40 // default is 528 (in px)
            });
        }
    }
});

Note that elem is a jQuery object inside link when jQuery.js is included in page before angular.js loads, otherwise it is a jQlite object with many (but not all) jQuery methods included

Sign up to request clarification or add additional context in comments.

1 Comment

<h3 big-text>My Header</h3> how does the big-text in this h3 tag if you don't mind?

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.