AngularJS: sharing of data between the directive and the external controller

advertisements

Basically I have:

  • directive,
  • data service with a promise
  • shared service to store data retrieved
  • control which should be updated whenever the directive invoke a data request on the data service.

Relevant snippets following.

Let's say the html inside directive gerSearkator contain a button which, when clicked,
executes the getResults() function defined inside the directive link.

It should call the service gerSearch, update the gerSearchResults service, which in turn
should update the $scope inside ResultsControllerand consequently the view binded to it.

But... why it does not work?

DIRECTIVE

angular.module('ger.directives').directive('gerSearkator', ['$http', 'gerSearch',
        'gerSearchResults',
function($http, search, searchResults) {
  return {
    restrict: 'A',
    scope: {
        ngModel: '='
    },
    replace: true,
    templateUrl: 'js/directives/searkator.html',
    link: function($scope, $element, $attrs) {
        $scope.src.getResults = function() {
            search.get($scope.src.params).then(
                // success
                function(data, status, headers, config) {
                    searchResults.resultsData = data;
                },
                // error
                function(data, status, headers, config) {
                    // manage error...
                }
            );
        };
    }
}
}]);

DATA SERVICE

 angular.module('ger.services').factory('gerSearch', ['$http', '$q', 'gerConfig',
 function($http, $q, config) {
    return {
        // "params" is an object
        get: function(params) {
            var d = $q.defer();

            $http.get(config.searchResultsPath, {params: params})
                .success(function(data, status, headers, config) {
                    d.resolve(data, status, headers, config);
                })
                .error(function(data, status, headers, config) {
                    d.reject(data, status, headers, config);
                }
            );

            return d.promise;
        }
    };
    }]);

SHARED SERVICE

angular.module('ger.services').factory('gerSearchResults', function() {
    return {
        resultsData: {}
    };
});

CONTROL TO UPDATE

 angular.module('ger').controller('ResultsController', function($scope, gerSearchResults) {
    $scope.searchResults = gerSearchResults.resultsData;
 });

HTML

<div ng-app="ger">
<!-- search box-->
<div ger-searkator></div>

<!-- tabs -->
<div class="row" ng-controller="ResultsController">
    <div class="col-lg-12">
        <code>{{searchResults}}</code>
    </div>
</div>
</div>

So far I have been able to solve the problem using:

$scope.$on('searchDataUpdated', function() {
    $scope.searchResults = gerSearchResults.resultsData;
});

But... is it the right way?

I added a plunker to better explain the problem (removing the use of $scope.$on('searchDataUpdated'... http://plnkr.co/edit/yorXy5SaAbAZKXRoo4vv?p=preview

Basically, when I click on the button I would like to populate the table with retrieved data.


Just put a watcher on service variable

angular.module('Gene').controller('ResultsController', function($scope, gerSearchResults) {

    $scope.searchResults = {};
    $scope.$watch(function(){
      return gerSearchResults.resultsData;
    },function(newvalue,oldvalue){
      if(newvalue===oldvalue)return;
      $scope.searchResults=newvalue;
    })
});