Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8751177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:59:46+00:00 2026-06-13T12:59:46+00:00

Within a directive definition , there’s an API for accessing $element and $attrs, so

  • 0

Within a directive definition, there’s an API for accessing $element and $attrs, so you can refer back to the element from which the directive was called. How would I access $element and $attrs when calling a custom function using a standard directive like ng-class?


EDIT: I understand that this is not an idiomatic approach. This question is applicable to rapid prototypes, which is a great use for many of Angular’s features. Just not the ones that are all about sustainability, separation of concerns, etc. My primary concern is velocity. Thus, being able to bang something out quickly with the built-in directives and a quick controller method can, in fact, be a virtue, and can win the opportunity to do a fuller and more proper implementation down the road…


In this case, I’m just adding a contextual .active class to a nav element, based on the value of $location.path(), as per this post and this one. However, in those examples you need to explicitly and redundantly pass a copy of the contents of the href attribute as an argument to the getClass() function you’re calling from ng-class. But isn’t there a way to programmatically access the href attribute from within getClass() and avoid redundantly passing identical content as an arg?

For example, a controller with a getClass() function the way I’d imagine it could work:

function navCtrl($scope, $routeParams) {
    $scope.getClass = function($element, $attrs) {
        if ($location.path().substr(0, path.length) == $attrs.href) {
            return "active"
        } else {
            return ""
        }
    }
}

Which you could then call simply and elegantly with:

<a ng-class="getClass()" href="/tasks">Tasks</a>

rather than:

<a ng-class="getClass('/tasks')" href="/tasks">Tasks</a>

(I recognize another option is to create a custom directive that does this, but for now I’d just like to figure out if it’s possible to access $attrs and/or $element from within a controller function that’s been called by a directive. Thanks!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:59:47+00:00Added an answer on June 13, 2026 at 12:59 pm

    You actually can do this… BUT YOU SHOULD NOT DO THIS. lol…

    Here’s how to do what you’re asking…

    In your markup, pass $event into your function.

    <a ng-click="test($event)" href="whatever">Click Me</a>
    

    Then in your controller, get the target element of the event:

    $scope.test = function($event) {
        alert($event.target.href);
    };
    

    Now here is why you probably shouldn’t do this:

    If you reference the DOM, or manipulate the DOM in your controller, you’re endangering dependency injection and also the separation of concerns from within the Angular structure. Sure you could still inject $event as a dependency if you were testing your function, but depending on what you’re doing inside of that function, you might still have ruined your DI, and you’re trying to make a controller do a directive’s work (which is to try to keep your controller from being tightly-coupled to your markup)

    That said, if ALL your doing is just getting a value, it’s probably fine, but you’re still coupling your controller to your markup to some degree. If you’re doing anything else with that DOM element, you’re off the reservation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With Apache the directive DirectoryIndex index along with DefaultType application/x-httpd-php within a particular vhost
Within Symphony CMS, I want to be able to have an XML document which
Within an asp.net MVC app, I'm trying to find rooms which have a guest/client,
Within a for loop, I'm using the following code to convert from one date
Within Google Spreadsheets, how can I retrieve the max and min values of a
Within Magento orders, there is the option to send the customer a notification to
I'm having an issue using the ng-show directive within an ng-repeat block. The boolean
I am working with an ASP.NET MVC project which was originally started from the
Are there any options (third-party or otherwise) to improve code folding within the Source
Within the context of a mixed C/Fortran application, is there a way to check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.