Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directive/Factory Not Working in Production

I have a directive that prints out flash messages for users. Everything works fine on my localhost but as soon as I test it out on Heroku, the flash message does not appear. Here is the controller.

angular.module("alerts")

.controller("AlertsController", alertController)

alertController.$inject = ['Flash']

function alertController(Flash) {
  var vm = this;

  vm.flash = Flash;
}

The directive...

angular.module("alerts")

.directive('flash', flash);

flash.$inject = ['$timeout'];

function flash ($timeout){
  return {
     restrict: 'E',
     replace: true,
     scope: {
         text: '=',
         type: '='
     },
     template: '<div id="flash_message" class="notification" ng-class="type"> {{text}} hello </div>',
     link: function(scope, element, attrs){
         $timeout(function(){
             element.remove();
         }, 5000);
     }
  }
}

And the factory that handles storing the flash messages.

angular.module("alerts")

.factory("Flash", flash)

function flash() {

  var messages = [];

  var results = {
    messages: messages,
    printMessage: printMessage,
    addMessage: addMessage
  }

  return results;

  function printMessage() {
    return results.messages
  }

  function addMessage(message) {
    results.messages.push(message);
  }
}

My html code...

<div ng-controller="AlertsController as alerts">
  <div ng-repeat="message in alerts.flash.messages">
    <flash type="message.type" text="message.text"></flash>
  </div>
</div>

No error appears on the console. The funny thing is the flash message is presented in the html doesn't load.

This is what is shown in localhost.

<div ng-repeat="message in alerts.flash.messages" class="ng-scope">
  <div id="flash_message" class="notification ng-binding ng-isolate-scope success" ng-class="type" type="message.type" text="message.text"> Your link has been copied! hello </div>
</div>

But on heroku production

<div ng-repeat="message in alerts.flash.messages" class="ng-scope"></div>

I'm creating the flash in my code via..

Flash.addMessage({type: "success", text: "Your link has been copied!"});

My question is, why does this not appear on my production code but it appears on my localhost and how do I fix it?

like image 296
thank_you Avatar asked May 28 '15 18:05

thank_you


People also ask

What is restrict option in directive?

Note: When you create a directive, it is restricted to attribute and elements only by default. In order to create directives that are triggered by class name, you need to use the restrict option. The restrict option is typically set to: 'A' - only matches attribute name. 'E' - only matches element name.

How directive works in AngularJS?

AngularJS directives are extended HTML attributes with the prefix ng- . The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.

What is attrs in AngularJS?

Using attrs you are able to access the attributes defined in your html tag like <fm-rating ng-model="$parent.restaurant.price" symbol="$" readonly="true"> So in this case you will have access to the symbol and readonly attributes.

How to call directive in AngularJS?

Inside the directive it is creating an updateMap() method on the scope object in the directive and then calling the setFn() method which is mapped to the $scope. setDirectiveFn() method by this line: <map set-fn="setDirectiveFn(theDirFn)"></map> in your HTML and this line: scope: { setFn: '&' } in your directive.


2 Answers

Of course, renaming the file name must have solved your problem. But the question is, why was it working earlier on localhost? I think on localhost all your javascript files loaded instantly and the function 'flash' of respective factory and directive got registered correctly.

But running on heroku or any other remote server, you will encounter problem as the controller or directive may register different 'flash' function than what they must register.

A plausible reason could be that angularjs's library file (from your server or cdn) was loaded into DOM a bit later than your files like controller.js, service.js or directive.js. Thats why the DOM didn't recognised the angular syntax of .controller() or .service(), etc at start. And consequently one of your global 'flash' function(of directive or factory) was overridden.

First of all I would suggest you to rename, which you have already done, and secondly, do not use global functions inside directive or factory.

I hope this explains.

like image 140
Vineet 'DEVIN' Dev Avatar answered Sep 24 '22 09:09

Vineet 'DEVIN' Dev


Renaming my file name and the internal function inside my directive solved the problem.

like image 44
thank_you Avatar answered Sep 25 '22 09:09

thank_you