2

Attempting to consolidate a function used across multiple controllers in an AngularJS project to a service/factory that I can call using $rootScope. The goal is to change the following function:

$scope.twitterRefresh = function(obj) {
      try { 
          var old = document.getElementById('twitterContainer');
          old.parentNode.removeChild(old);
      }
      catch (err) {};
      var html = "<div id='twitterContainer'>"+$scope.game[0]['twitterEmbed']+"</div>";
    var elem = $compile(html)($scope);
    angular.element(document.getElementById('twitterEmbed')).append(elem);
};

into a combination of a $rootScope & service to avoid having to include the service in every controller that requires the function. The following code, however, isn't working:

app.factory('twitterRefresh', ['$document', '$compile', '$rootScope',
    function(obj,$document, $compile, $rootScope) {
  try { 
      var old = document.getElementById('twitterContainer');
      old.parentNode.removeChild(old);
  }
  catch (err) {};
  var html = "<div id='twitterContainer'>"+obj+"</div>";
  var scope = $rootScope.$new();
  var elem = $compile(html)(scope);
  angular.element(document.getElementById('twitterEmbed')).append(elem);
}]);

app.run(function($rootScope, twitterRefresh) {
    $rootScope.twitterRefresh= function(obj){
      twitterRefresh(obj);
    }
});

where the following function is called in the controller..

   $scope.twitterRefresh = $rootScope.twitterRefresh($scope.game[0]['twitterEmbed']);
1
  • 2
    $scope.twitterRefresh() = $rootScope.twitterRefresh.... did you mean to put the brackets () in? $scope.twitterRefresh = function() { $rootScope.twitterRefresh($scope.game[0]['twitterEmbed']); } looks more useful to me Commented Apr 10, 2018 at 21:16

2 Answers 2

1

Don't define the function on each controller $scope, just leave it on $rootScope, and use it when needed elsewhere by injecting $rootScope into your controller and making it available that way.

   //something like this

   app.run(function($rootScope, $compile) {
       $rootScope.twitterRefresh= function(obj, scope){
           $compile(obj)(scope);
       }
   });
Sign up to request clarification or add additional context in comments.

3 Comments

i believe the question I have is how to pass the variable $scope.game[0]['twitterEmbed'] to either the service/factory or the rootScope function
secondly, how should a $rootScope function correctly call the $compile service
To correctly call $compile you would need to pass the $scope instance to the $rootScope function you have defined. If your $rootScope function is in app.js run function, make sure you inject $compile service, and pass the $scope instance from the controller. That should work.
0

Try this way:

app.factory('twitterRefresh', ['$document', '$compile', '$rootScope',
  function($document, $compile, $rootScope) {
    return function (obj) {
      try { 
        var old = document.getElementById('twitterContainer');
        old.parentNode.removeChild(old);
      }
      catch (err) {};
      var html = "<div id='twitterContainer'>"+obj+"</div>";
      var scope = $rootScope.$new();
      var elem = $compile(html)(scope);
      angular.element(document.getElementById('twitterEmbed')).append(elem);
    }
  }
]);

Comments

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.