【问题标题】:Calling Angularjs Function from a different javascript/Jquery Function从不同的 javascript/Jquery 函数调用 Angularjs 函数
【发布时间】:2017-08-04 17:52:37
【问题描述】:

我有两个 js 文件,app.js 和 datepicker.js。我与这里的 html 文件无关。在 datepicker.js 中有一个名为 clickApply() 的 JavaScript 函数。我想从 clickApply() 函数调用 app.js 文件中的另一个 submitDetails() 函数。怎么做?

angular.module('ctrl').controller('MyCtrl', ["$scope","$rootScope", function($scope, $rootScope){
                
$rootScope.submitDetails=function() 
}]);//inside app.js

clickApply: function(e) { //inside datepicker.js
            
             angular.element('#span').on("click",submitDetails());
          
            this.hide();
            this.element.trigger('apply.daterangepicker', this);

        }

【问题讨论】:

  • 只需像这样调用你的函数 submitDetails();
  • 你的 clickApply 函数写在哪里?
  • 我认为这可能是stackoverflow.com/questions/23648458/…的副本
  • 如果您控制日期选择器的来源,我会将submitDetails 函数作为回调传递给日期选择器,然后在clickApply 函数中调用回调。

标签: javascript jquery angularjs html css


【解决方案1】:

没有人喜欢在角度范围之外执行角度函数。如果您想在 AngularJS 中实现 jQuery 或任何其他外部插件,请将其包装在指令中并使用它。

以下代码仅用于debugging 目的。

将您的 clickApply 函数签名更改为以下:

  • 要访问scope,您可以使用angular.element(element).scope()
  • 对于rootScope,您可以使用 angular.element('body').scope().$root`

clickApply: function(e) { //inside datepicker.js
  angular.element('#span').on("click", function(e) {
    // if you want to access rootScope
    var rootScope = angular.element('body').scope().$root;
    rootScope.submitDetails();
    
    // if you want to access current scope
    // var scope = angular.element(e.target).scope();
    // scope.submitDetails();
  });
  this.hide();
  this.element.trigger('apply.daterangepicker', this);
}

【讨论】:

    猜你喜欢
    • 2014-07-02
    • 1970-01-01
    • 2013-07-27
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多