1

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.

3
  • question is little unclear and code is not clear kindly share a fiddle please Commented Sep 23, 2013 at 14:48
  • ok, I will post a fiddle as soon as possible... Commented Sep 23, 2013 at 14:56
  • I have added a similar working copy for you where updating angular service in directive will reflect in controller plnkr.co/edit/IcthzM059QM1rkkyhtYA?p=preview Commented Sep 23, 2013 at 19:35

2 Answers 2

3

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;
    })
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanx! I guess so far this is a best solution than triggering a $rootscope broadcast. I was just wondering if was not possible to have an "automatic" two way binding.
0

Not sure what is the exact issue, but the reason could be that you are replacing the object reference for resultData with a new object.

searchResults.resultsData = data;

Can you try something like

searchResults.resultsData.length=0;
angular.forEach(data,function(item){
    searchResults.resultsData.push(item);
});   

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.