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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:53:37+00:00 2026-06-13T10:53:37+00:00

I am implementing a web app using angularjs (first time for me). One of

  • 0

I am implementing a web app using angularjs (first time for me). One of the feature I would like to provide the user with is to create a div in the main window when the user click on an image in the menu.

For that, I have created a directive as shown below.

var platform = angular.module('platform', []);
    
platform.directive('addElement', function() {
        
  return function (scope,element, attrs){
        var createElement;
        
        createElement = function(){
            $('<div>Test</div>').appendTo('#body-main-window');
        };

        
        $(this).on('click',createElement);
    } 
});

And here is the following html code:

<img class = "imgMenu" src="styles/img/logo_add.png" add-element />

The problem is that the onClick event triggered on an element looks to be propagated to all its ancestors. Indeed, wherever I click on, the div is created. It is not the expected behaviour since the div should be created only when the user click on image where the directive is.

Additionally, when clicking, the directive is executed as many times as there are images with the directive attribute.

Here is a fiddle to illustrate my problem : http://jsfiddle.net/6Ku7D/10/

One solution would be to use the ng-click directive but it will imply doing some DOM modifications within the controller (and it is not the proper way to achieve my goal). Directive is thus the solution but I am getting stuck.

EDIT (EXTRA INFORMATION)

Actually, it is just the skeleton of what I want to do. In a nutshell, elements I want to click on are in the menu of my app. These elements are simply names of some objects. By clicking on them, I want to create a div that will not contain only the name but other information on this object that can be edited. Moreover, the div I will create should be draggable (using jquery-ui) and finally, I should be able to "link" these divs to create a workflow. Actually, what I want to do is something quite similar to yahoo pipes (providing a user-friendly interface for mashup/workflow edition) but dedicated to researchers in computer science (data mining). Objects will be algorithms, datasets or visualization techniques that have been first imported via a web service I have developped. Consequently, it is not as simple as "adding a value to an existing array". Do you still think I should use ng-click?

  • 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-13T10:53:38+00:00Added an answer on June 13, 2026 at 10:53 am

    This is a Jquery event issue…

    try using stopPropagation() to keep the event from bubbling.

    platform.directive('addElement', function() {
    
      return function (scope,element, attrs){
            var createElement;
    
            createElement = function(e){
                $('<div>Test</div>').appendTo('#body-main-window');
               e.stopPropagation();
            };
    
    
            $(this).on('click',createElement);
        } 
    });
    

    EDIT

    I see what you’re trying to do, and here is a working Plunk that does what you’re trying to do… that said, I don’t think you’re going about this the best way.

    If you want to add a new value to a list of values in Angular, I think you’re going about it the hard way. Then again I don’t know what your end goal is… but try this instead:

    Here’s a plunk for my suggested solution

    <body ng-controller="MainCtrl"> 
        <div id="menu">
            <div ng-repeat="item in listTest" class="toAddIcon" ng-click="addResult()">
                {{item}}, click here to add a div to results
            </div>                          
        </div>
    
        <hr/>
        <div id="displayResult"> 
            <header> -- Results -- </header>   
            <div ng-repeat="result in results">{{result}}</div>
        </div>
    </body>
    

    The JS:

    app.controller('MainCtrl', function($scope) {
      $scope.listTest = ['Peter','Bob','Susan'];   
      $scope.results = [];
    
      $scope.addResult = function(){
        $scope.results.push('TEST');
      };
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm Implementing a simple web app using Django, I want to get user's location
Before implementing j_security_check using MySQL realm authentication in my web app. I had the
It seems like implementing web-app like twitter/facebook-wall needs 1 huge feeds relational table (+
I'm implementing a web app, which uses sessions. I'm using GWT and app engine
I am implementing a web app using symfony 2. It has a main page
I'm going to be implementing a Web app using Rails with MySQL running on
I've been teaching myself Spring 3.0 by implementing a JavaEE Web-app using: Tomcat 7
I'm implementing a web app using web2py and jQuery. I want to use the
I'm implementing a small web app widget that shows a legend/key for a diagram
I'm making an app that is requesting data from a Web Service (implementing Soap).

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.