如何在服务中创建公共方法

时间:2016-08-04 16:45:15

标签: javascript angularjs

我一直在使用这个开源核心,想要添加我自己的功能。

这是服务:

"use strict";
define([
  'mediastream/webrtc'
  ], function(WebRTC) {
    return function() {
      this.testcall = function() {
        console.log(1);
    }
  };
});

这就是我如何称呼它:

 "use strict";
  define(['underscore', 'text!partials/buddylist.html'], function(_, template) {

    // buddyList
    return ["buddyList", "api", "webrtc", "contacts", "sourceDistribution", function(buddyList, api, webrtc, contacts, sourceDistribution) {


  //console.log("buddyList directive");

  var controller = ['$scope', '$element', '$attrs', function($scope, $element, $attrs) {

...

        $scope.doViewBroadcast = function(id) {
          sourceDistribution.testcall();
        };

那么如何调用testcall()函数?

1 个答案:

答案 0 :(得分:0)

这有效:

"use strict";
define([
  'mediastream/webrtc'
], function(WebRTC) {

  var Testi = function(api) {
    var broadCastTypes = ["call", "video"];
  }

  Testi.prototype.doCall = function(api) {
    console.log(1);
  }

  return ["api", function(api) {
    return new Testi(api);
  }];
});

致电:sourceDistribution.doCall(id);