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 8972347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:11:53+00:00 2026-06-15T18:11:53+00:00

I require a custom DOM element that accepts mouse clicks and whose state is

  • 0

I require a custom DOM element that accepts mouse clicks and whose state is dependant on the model:-

<card ng-repeat="card in cards" x="card.x"
    y="card.y" color="card.color" on-click="test_click('b')">
</card>

I am able to build a custom directive that binds to a controller’s scope variables through its DOM attributes and use them to alter its view. I have this working by allowing the directive to inherit its parents scope:

app.directive('card', function ($timeout) {
    return {
        restrict:'E',
        link:function (scope, element, attrs) {
            element.addClass('card');

            element.click(function(){
                scope.onClick()
            });

            scope.$watch(attrs.x, function (x) {
                element.css('left', x + 'px');
            });
            scope.$watch(attrs.y, function (y) {
                element.css('top', y + 'px');
            });
            scope.$watch(attrs.color, function (color) {
                element.css('backgroundColor', color);
            });
        }
        /*
        ,scope:{
            x:'=',
            y:'=',
            color:'=',
            onClick: "&"
        }
        */
    };
});

I am able to get a mouse click event to propagate up to the controller by creating an isolated scope and doing some rewiring (by commenting the scope in above).

However, I am unable to get both behaviours working at the same time.

I presume I need to get the x variable bound to the attribute value, which is what I have tried to do. But even by trying every combination of syntax I can think of, I just can’t seem to get it working.

Here is the complete case jsfiddle

  • 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-15T18:11:54+00:00Added an answer on June 15, 2026 at 6:11 pm

    If I understand what you’re trying to do, you can use ng-style instead of $watch()es, and ng-click instead of element.click():

    <card ng-repeat="card in cards" ng-click="test_click('b')"
      ng-style="{left: card.x, top: card.y, 'background-color': card.color}" >
    

    Fiddle

    When we use any of the pre-built Angular directives — e.g., ngStyle, ngRepeat, ngClass, ngSwitch, ngShow — and tie them to models, Angular does the watch()ing for us.

    (I don’t understand why it only works if I include jQuery. I don’t see any jQuery-specific methods being called.)

    Update: I figured out how to make it work without jQuery — add ‘px’ to the ng-style:

    <card ng-repeat="card in cards" ng-click="test_click('b')
      ng-style="{left: card.x + 'px', top: card.y + 'px', 'background-color': card.color}" ">
    

    Updated fiddle.

    I guess jQuery is more forgiving somehow if we leave off the ‘px’.

    To the second fiddle, I also added a “move card #1 down 200px” ng-click/hyperlink, to prove that changing the model results in Angular automatically updating the view for us.

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

Sidebar

Related Questions

I'm trying to create a custom overlay that basically just contains a HTML element
I am working a project that will require a custom wizard to help a
i have a validation class which needs improving. If I require some custom validation
I'm interacting with a custom APEX service which obviously require OAuth authentication. I can
For example I want to use custom logger: logger = require('basic-logger'), logger.setLevel('info') var customConfig
I installed slim gem to a custom directory of my own but require 'slim'
I am designing a custom button with that requires me to overlay a UIButton
The application I'm working on has a custom UI that required me to remove
I've created a WCF DataService and for this service I require some custom authentication
I have a Rakefile that looks like this: require 'rspec/core/rake_task' desc Run all RSpec

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.