指令的角度API

angular API for directives

本文关键字:API 指令      更新时间:2023-09-26

您认为创建公开服务的指令的方法是什么?

示例有一个指令要做,然后获取UI的请求:

<api-http id="clients" uri="rest/clients"></api-http>
<button ng-click="clients.get()">search clients</button>
<table>
<tr ng-repeat="client in clients.results.data"> 
<td>{{client.name}}</td>
</tr>
</table>

api http是一个通过ui公开api rest的策略,按钮我调用id中公开的api,调用get将结果存储在变量result下的api中,并在下表中显示resultsenter代码,他们认为这种方法是什么?

这并不能免除控制器的使用,因为我们可以有一个控制器和控制器来调用api可视化组件。

这样的指令可能看起来像这样:

.directive('apiHttp', function($http) {
  return {
    link: function(scope, element, attrs) {
      scope[attrs.id] = {
        results: null,
        get: function() {
          $http.get(attrs.uri).then(function(response) {
            this.results = response;
          }.bind(this))
        }
      }
    }
  };
});

演示:http://plnkr.co/edit/30jPmH0gcGkjGlzVJuGd?p=preview