Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何调用一个函数作为字符串传递的参数调用另一个函数_Javascript_Angularjs - Fatal编程技术网

Javascript 如何调用一个函数作为字符串传递的参数调用另一个函数

Javascript 如何调用一个函数作为字符串传递的参数调用另一个函数,javascript,angularjs,Javascript,Angularjs,我正试图在控制器中调用一个服务函数。该服务还调用同一服务中的另一个函数。当我可以从控制器调用该服务时,我只能在显式通过第二个服务的情况下使其工作。我想做的是能够传递一个字符串来调用第二个服务。它看起来更干净,更容易阅读。但我不知道该怎么做 服务: function $cmCallOut($timeout, $cmAutoscroll) { var service = { runCallOut: runCallOut, unavailable: unavailab

我正试图在控制器中调用一个服务函数。该服务还调用同一服务中的另一个函数。当我可以从控制器调用该服务时,我只能在显式通过第二个服务的情况下使其工作。我想做的是能够传递一个字符串来调用第二个服务。它看起来更干净,更容易阅读。但我不知道该怎么做

服务:

function $cmCallOut($timeout, $cmAutoscroll) {

    var service = {
      runCallOut: runCallOut,
      unavailable: unavailable,
      destroy: destroy,
      create: create,
      update: update
    };
    return service;

    function runCallOut(recordId, callout) {
      // Wrap in timeout to wait for DOM to load before grabbing the element
      $timeout(function() {
        // Get element which to perform callout animation on
        var element = angular.element('[record-id=' + recordId + ']');
        // Get the elements coords so we can scroll to it
        var rect = element[0].getBoundingClientRect();
        // Scroll to element
        $cmAutoscroll.toTop(rect.top);
        // Run animation
        callout(element);
      });
    }

    function unavailable(element) {
      element.velocity('callout.shake');
    }

    function destroy(element) {
      element.velocity('transition.expandOut', {duration : 350});
    }

    function create(element) {
      element.css('display', 'none');
      element
      .velocity('transition.expandIn', {duration : 450, display: 'block', delay: 350})
      .velocity('callout.flash');
      $timeout(function() {
        element.css('transform', 'none');
      }, 2000);
    }

    function update(element) {
      element.velocity('callout.flash', {duration: 1500, delay: 350});
    }

  }
$cmCallOut.runCallOut(chargeId, $cmCallOut.create);
$cmCallOut.runCallOut(chargeId, 'create');
控制器:

function $cmCallOut($timeout, $cmAutoscroll) {

    var service = {
      runCallOut: runCallOut,
      unavailable: unavailable,
      destroy: destroy,
      create: create,
      update: update
    };
    return service;

    function runCallOut(recordId, callout) {
      // Wrap in timeout to wait for DOM to load before grabbing the element
      $timeout(function() {
        // Get element which to perform callout animation on
        var element = angular.element('[record-id=' + recordId + ']');
        // Get the elements coords so we can scroll to it
        var rect = element[0].getBoundingClientRect();
        // Scroll to element
        $cmAutoscroll.toTop(rect.top);
        // Run animation
        callout(element);
      });
    }

    function unavailable(element) {
      element.velocity('callout.shake');
    }

    function destroy(element) {
      element.velocity('transition.expandOut', {duration : 350});
    }

    function create(element) {
      element.css('display', 'none');
      element
      .velocity('transition.expandIn', {duration : 450, display: 'block', delay: 350})
      .velocity('callout.flash');
      $timeout(function() {
        element.css('transform', 'none');
      }, 2000);
    }

    function update(element) {
      element.velocity('callout.flash', {duration: 1500, delay: 350});
    }

  }
$cmCallOut.runCallOut(chargeId, $cmCallOut.create);
$cmCallOut.runCallOut(chargeId, 'create');
想这样称呼它:

function $cmCallOut($timeout, $cmAutoscroll) {

    var service = {
      runCallOut: runCallOut,
      unavailable: unavailable,
      destroy: destroy,
      create: create,
      update: update
    };
    return service;

    function runCallOut(recordId, callout) {
      // Wrap in timeout to wait for DOM to load before grabbing the element
      $timeout(function() {
        // Get element which to perform callout animation on
        var element = angular.element('[record-id=' + recordId + ']');
        // Get the elements coords so we can scroll to it
        var rect = element[0].getBoundingClientRect();
        // Scroll to element
        $cmAutoscroll.toTop(rect.top);
        // Run animation
        callout(element);
      });
    }

    function unavailable(element) {
      element.velocity('callout.shake');
    }

    function destroy(element) {
      element.velocity('transition.expandOut', {duration : 350});
    }

    function create(element) {
      element.css('display', 'none');
      element
      .velocity('transition.expandIn', {duration : 450, display: 'block', delay: 350})
      .velocity('callout.flash');
      $timeout(function() {
        element.css('transform', 'none');
      }, 2000);
    }

    function update(element) {
      element.velocity('callout.flash', {duration: 1500, delay: 350});
    }

  }
$cmCallOut.runCallOut(chargeId, $cmCallOut.create);
$cmCallOut.runCallOut(chargeId, 'create');

因为您想要的函数是您在上面声明的服务变量上的键,所以您可以这样调用它:

service[callout](element);
所以


关联数组
var func={create:$cmCallOut.create}怎么样$cmCallOut.runCallOut(chargeId,func['create'])需要在第一个中注入第二个服务并从Controlleri中删除该注入如果我理解正确,您希望为第二个参数传递一个字符串,并根据该字符串决定调用哪个函数。在这种情况下,您可以使用“switch”,添加不同的案例,并根据发送的参数调用相应的函数