AngularJS:在指令和外部控制器之间共享数据 [英] AngularJS: sharing data between directive and external controller

查看:85
本文介绍了AngularJS:在指令和外部控制器之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有:

  • 指令
  • 承诺的数据服务
  • 共享服务以存储检索到的数据
  • 控件,只要该指令在数据服务上调用数据请求,就应更新该控件.

有关的摘要如下.

假设html内部指令gerSearkator包含一个按钮,当单击该按钮时,
执行指令链接中定义的getResults()函数.

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

它应该调用服务gerSearch,更新gerSearchResults服务,该服务反过来
应该更新ResultsController内部的$scope,从而将视图绑定到该视图.

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?

指令

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...
                }
            );
        };
    }
}
}]);

数据服务

 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;
        }
    };  
    }]);

共享服务

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

控制更新

 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?

我添加了一个插件来更好地解释该问题(删除了$ scope.$ on('searchDataUpdated'... http://plnkr.co/edit/yorXy5SaAbAZKXRoo4vv?p=preview

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;
    })
});

这篇关于AngularJS:在指令和外部控制器之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆