title: Angular JS: How to pass unknown values to 3rd party scripts in Controller
body: <p>So I have a project that requires me to have a third party integration with a commenting service. In the controller I am using a factory that gets the JSON feed for the articles. I have the 3rd party JavaScript code as part of the init() function of the controller, so when each story detail view loads, the comments section refreshes for that story. I am using Routing and Views by reusing the index.html page. So there is a template that is called for article details. That template relies on this controller for which I show code below.</p>
  
  <p>The third party code asks for a unique id for each story passed as part of a "params" options. I am using $routeParams.itemId to supply that unique id + a custom string to identify the section and avoid the same ID being duplicated for another story (as we have 3 feeds), so "Art" for articles, "Vid" for videos etc. What I really want to pass into this unique id option is a value that exists inside my JSON feed which is PostID. </p>
  
  <p>So the JSON would be stories.articles.postId for the current article being viewed. Is there a way to supply that JSON value at this stage? We need to use PostID because we need to maintain the comments in the web site and the comments in this angular app in sync for each article and PostID is a field that can identify them uniquely and is shared between both environments. The Controller code follows:</p>
  
  <pre><code>(function() {
  
   var DetailsController = function($scope, $http, $routeParams, $sce, dataFactory) {
   storyId = $routeParams.itemId;
   uniqueId = "Art" + $routeParams.itemId;
   nextItem = $routeParams.itemId++;
   $scope.stories = [];
  
  function init() {
    dataFactory.getStories().success(function(stories) {
      $scope.stories = stories.articles;
      $scope.whichItem = storyId; 
      $scope.nextArticle  = nextItem;
      $scope.totalList = stories.articles.length;
  
        $scope.SkipValidation = function(value) {
            return $sce.trustAsHtml(value);
          };    
    })
    .error(function(data, status, headers, config) {
        $log.log(data.error + ' ' + status);
    });
  
    $scope.todaysDate = new Date();   
  
    var params ={
      categoryID: 'Articles',
      streamID: uniqueId,
      version: 2,
      containerID: 'commentsDiv',
      cid:''
    }
    gigya.comments.showCommentsUI(params);
  }
   init();
  
   };
  
   DetailsController.$inject = ['$scope', '$http', '$routeParams', '$sce', 'dataFactory'];
  
   angular.module('myApp')
     .controller('DetailsController', DetailsController);
  
    }());
  </code></pre>
  
  <p>So based on the answer that Joao provided below, now I have a directive, slightly modified to use route params like this:</p>
  
  <pre><code>angular.module('myApp').directive('comments', ['$routeParams','dataFactory', function ($routeParams, dataFactory) {
      var linker = function (scope, element, attrs) {
          storyId = $routeParams.itemId;
          whichItem = storyId;
              var params = {
               categoryID: 'Articles',
               streamID: scope.uniqueId,
               version: 2,
               containerID: '=',
               cid:''
             }
          gigya.comments.showCommentsUI(params);
          };
       return {
         restrict: "E",
         replace: true,
         link: linker,
         template: '&lt;div id="commentsDiv"&gt;&lt;/div&gt;',
         scope: {
           uniqueId: '='
         }
      };
  
   }]);
  </code></pre>
  
  <p>It is still not working but I am close. The HTML tamplate needs to reference the current article inside a data set that I am cycling through (the JSON data of 25 articles) so it looks like this:</p>
  
  <pre><code>&lt;comments unique-id="stories[whichItem].postId"&gt;&lt;/comments&gt;
  </code></pre>
  
  <p>However this fails to evaluate to an actual value. I think is almost working but I am ending up with an article with a route parameter of -1 which makes no sense.</p>
  
  <p>Maybe this is impossible to do...</p>