【问题标题】:How to call other function after angular JS directive's reference function get calledangularJS指令引用函数被调用后如何调用另一个函数
【发布时间】:2021-02-05 13:04:54
【问题描述】:
<DirectiveName value1="someValue"  functionRef="someFunction()"></DirectiveName>
angular.module('Name', ['Dependencies']).
        directive('DirectiveName',
                function($timeout) {
                    return {
                        restrict: 'AE',
                        replace: 'true',
                        template : 'HTML Template',
                        scope: {
                            variable1: '=value1',
                            Variable2: '&functionRef',
                        },
                        link: function(scope, watch){
                            
                        }
                    };
                });
  1. 我正在使用这个 Angular JS 自定义指令,我们从 HTML 传递方法的引用以被调用。
  2. 现在我们想在执行引用函数(即变量 2)之后执行另一个函数

我们不能从 HTML 或父控制器调用通用函数,因此我们需要在执行引用函数(即变量 2)后从 angular 指令调用通用函数

【问题讨论】:

    标签: javascript angularjs angularjs-directive


    【解决方案1】:
    I think you are still using angular 1. If that is true then check below link. Hope it will help you. If not then 'output' (eventEmitter) present in angular version > 2 
    
    1.http://www.binaryintellect.net/articles/5d8be0b6-e294-457e-82b0-ba7cc10cae0e.aspx
    2.https://stackoverflow.com/questions/47639275/using-emit-to-pass-a-function-from-child-to-parent-in-angularjs
    

    【讨论】:

      【解决方案2】:
      angular.module('Name', ['Dependencies']).
              directive('DirectiveName',
                      function($timeout) {
                          return {
                              restrict: 'AE',
                              replace: 'true',
                              template : 'HTML Template',
                              scope: {
                                  variable1: '=value1',
                                  Variable2: '&functionRef',
                              },
                              link: function(scope, element, attr){
                                  scope.Variable2 = function(){
                                      
                                      scope.$parent.$eval(attr.functionRef);
                                      
                                      /* other statement goes here */ 
                                  }
                              }
                          };
                      });
      

      覆盖函数有效,使用 $eval() 从覆盖函数内部调用引用函数

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多