AngularJS:如何在 AngularJS 中使用或注入第三方库

pun*_*bit 5 javascript dependency-injection angularjs deployd

我是 Angular 和 Deployd 的新手,想知道如何一起使用它们。

我发现 Deployd 网站中的示例很好,但它只消耗其余 API 数据,我想了解如何将 Deployd 作为 AngularJS 中的服务。例如,通过部署中可用的收集事件,使所有客户端 API 保持最新的收集最新数据。

我想出了下面的示例,我们可以看到我正在使用 $resource 来使用其余 api,但在控制器“MyCtrl”内,我正在调用 dpd,我想用它来利用功能,例如http://docs.deployd.com/docs/collections/notifying-clients.md

我真的很想看到一些例子,或者对此有任何建议!

感谢您的浏览:)

angular.module('questions', ['ngResource'])

.factory('Deployd', function(dpd){
  return dpd;
})

.factory('EntriesService', function($resource){
  return $resource('/entries', {});
})

.controller('MainCtrl', ['$scope', 'EntriesService', function($scope, EntriesService) {

  $scope.title = "Q&A Module";

  $scope.entries = [];

  EntriesService.query(function(response){
    $scope.entries = response;       
  });

  $scope.addMessage = function() {
    $scope.entries.push({
        author: "myAuthor",
        message: $scope.message
    });

    EntriesService.save({
        author: "myAuthor",
        message: $scope.message
    });

  };

  dpd.comments.get(function(comments, error) {
    comments.forEach(function(comment) {
      console.log(comment);
    });
  });

}]);
Run Code Online (Sandbox Code Playgroud)

thi*_*utg 5

如果您使用带有组件和 ES2015/ES6 的 AngularJS 1.5+,您可以使用常量注入外部库。

例如,要将d3.js注入 AngularJS 组件中:

首先使用 d3 安装npm install d3 --save,然后将其导入到您的应用程序模块中,然后将其作为常量注入到您的组件中。

在应用程序模块中:

import * as d3 from 'd3';
import { MyComponent } from './my-component/my-component';

export default angular.module('app', [])
    .constant('d3', d3)
    .component('myComponent', MyComponent)
    .name;
Run Code Online (Sandbox Code Playgroud)

我的组件中:

// Controller
class MyController {
    constructor(d3, $element) {
        this.d3 = d3;
        this.$element = $element;
    }

    $onInit() {
        // $element is the jqLite or jQuery component's element and 
        // $element[0] is the DOM node element
        this.d3.select(this.$element[0]).append('p').text('Hello world');
    }
}

// Component
export var MyComponent = {
    template: require('./my-component.html'),
    controller: MyController
};
Run Code Online (Sandbox Code Playgroud)


pun*_*bit 4

我找到了解决方案。这可能对其他人将来有帮助:

angular.module('questions', ['ngResource'])

.factory('Deployd', function(){
  return dpd;
})

.factory('EntriesService', function($resource){
  return $resource('/entries', {});
})

.controller('MainCtrl', ['$scope', 'EntriesService', 'Deployd', function($scope, EntriesService, Deployd) {

  $scope.title = "Q&A Module";

  $scope.entries = [];

  EntriesService.query(function(response){
    $scope.entries = response;        
  });

  $scope.addMessage = function() {

    var author = "myAuthor";
    var message = $scope.message;

    Deployd.entries.post({
      author: author,
      message: message
    }, function(comment, error) {

      if (error) {
        return showError(error);
      }

      $scope.entries.push({
        author: author,
        message: message
      });

    });

  };

  Deployd.entries.on('create', function() {
    EntriesService.query(function(response){
      $scope.entries = response;        
    });
  });

}]);
Run Code Online (Sandbox Code Playgroud)