0

I'm currently learning AngularJS and I want to create an module for notifications, but I can't find any helping tutorials.

I want all my notifications add into a div, which I need to create at first. So here comes my first question. Where do I have to append the element into the DOM so the code is still valid angular-code.

<!DOCTYPE html>
<html>
    <head>
        ...
    </head>
    <body>
        ...
        <div class="notification-center"> //Element I need to create
            ... //Notifications come here
        </div>
    </body>
</html>

I solved it by appending the div inside the module.run() function, but somehow I don't think it should be there

var notification = angular.module('notification', []);

notification.run([function(){
    var el = angular.element('#notificationcenter');

    if(!angular.element(el).length){
        el = angular.element('<div>').attr('id', 'notificationcenter').text('{{ text }}').appendTo(angular.element('body'));
    }
}]);

For creating the notifications I've written an factory where i append the another div inside the #notificationcenter div.

notification.factory('notification', [function(){
    var defaults = {
        duration: 5000
    };

    return function(text, settings){
        var element = angular.element('<div>').addClass('notification').text(text).appendTo(angular.element('#notificationcenter'));
    };
}]);

Basically this works aswell, but when I want to create a directive for the notification, it won't apply. I've seen examples where it's done with the $compile method, but it always happened inside another directive or controller as an scope is needed, which I don't have inside a factory.

What am I doing wrong?

2
  • 2
    docs.angularjs.org/guide/directive Commented Oct 26, 2015 at 23:29
  • 1
    using angular.element is no different from using JQuery. if you are planning on making a directive that manipulates the DOM, it should use a template, not angular.element. and factories should never be responsible for DOM code. Commented Oct 26, 2015 at 23:33

1 Answer 1

2

Define a controller, which maintains an array of notifications:

  notification.controller('NotificationController', function() {
      var ctrl = this;
      ctrl.notificationArray = [ ];
      ctrl.addNote = function (note) {
          ctrl.notificationArray.push(ctrl.notificationArray.length + ". " + note);
      };
  });

Then in HTML, perform an ng-repeat on this array:

    <div ng-controller="NotificationController as ctrl">
        <div class="notification-center" ng-if="ctrl.notificationArray.length>0">
            <div class="notification" ng-repeat="note in ctrl.notificationArray">
               {{note}}
            </div>
        </div>
        <button ng-click="ctrl.addNote('another note');">Add note</button>
    </div>
Sign up to request clarification or add additional context in comments.

2 Comments

thats a good idea, but where do i have to append the "wrapper"-div? Inside the module.run() function? Because I want to reuse the module and I dont want to care about writing the div manually into the template(s)
To make this reusable, you should create a "directive". Please follow an AngularJS tutorial to learn how to do this (docs.angularjs.org/guide/directive).

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.