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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:46:09+00:00 2026-06-17T19:46:09+00:00

Using AngularJS and UI Bootstrap, I want to dynamically add alerts to DOM. But

  • 0

Using AngularJS and UI Bootstrap, I want to dynamically add alerts to DOM. But if I dynamically add an <alert> element to DOM, it’s not compiled automatically. I tried to use $compile but it doesn’t seem to understand tag names not present in core AngularJS. How can I achieve this? Is it even the right way to “manually” add elements to DOM in services?

See Plunker. The alert in #hardcodedalert is compiled and shown correctly but the contents of #dynamicalert are not being compiled.

Edit:

I’d later want to have alerts shown on different context and locations on my web page and that’s why I created a constructor function for the alerts, to have a new instance in every controller which needs alerts. And just for curiosity’s sake, I was wondering if it’s possible to add the <alert> tags dynamically instead of including them in html.

  • 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-17T19:46:11+00:00Added an answer on June 17, 2026 at 7:46 pm

    I’ve updated your plunker to do what you’re trying to do the “angular way”.

    There are a few problems with what you were trying to do. The biggest of which was DOM manipulation from within you controller. I see you were trying to offset that by handling part of it in the service, but you were still referencing the DOM in your controller when you were using JQuery to select that element.

    All in all, your directives weren’t compiling because you’re still developing in a very JQuery-centric fashion. As a rule of thumb you should let directives handle the adding and removing of DOM elements for you. This handles all of the directive compiling and processing for you. If you add things manually the way you were trying, you will have to use the $compile provider to compile them and run them against a scope… it will also be a testing and maintenance nightmare.

    Another note: I’m not sure if you meant to have a service that returned an object with a constructor on it, so I made it just an object. Something to note is that services are created and managed in a singleton fashion, so every instance of that $alertService you pass in to any controller will be the same. It’s an interesting way to share data, although $rootScope is recommended for that in most cases.

    Here is the code:

    app.factory('alertservice', [function() {
      function Alert() {
        this.alerts = [];
        this.addAlert = function(alert) {
          this.alerts.push(alert);
        };
      }
      return {
        Alert: Alert
      };
    }]);
    
    app.controller('MainCtrl', function($scope, alertservice) {
      var myAlert = new alertservice.Alert();
      $scope.alerts = myAlert.alerts;
      $scope.add = function() {
        myAlert.addAlert({"text": "bar"});
      };
    });
    

    Here are the important parts of the updated markup:

      <body ng-controller="MainCtrl">
        <div id="dynamicalert">
          <alert ng-repeat="alert in alerts">{{alert.text}}</alert>
        </div>
        <button ng-click="add()">Add more alerts...</button>
      </body>
    

    EDIT: updated to reflect your request

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

Sidebar

Related Questions

I am using Angularjs for a web application. I have tried searching to find
I'm using AngularJS with Rails and creating dynamic nested form items which are not
I'm trying to build web app that dynamically load interfaces, using angularJS. I found
I want to achieve read/write access control for select input using angularjs and angular-ui
I wish to display data from mysql database using angularJS but all in vain.
I am using ng-view to include AngularJS partial views, and I want to update
I've been using directives in AngularJS which build a HTML element with data fetched
I'm using AngularJS with a third party service that generates html responses. I want
I know that this is usually not the way to use AngularJS but I
When using server-side templating and client-side angularjs, I'm not able to get angularjs to

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.