【问题标题】:Check condition in angularjs检查angularjs中的条件
【发布时间】:2017-12-19 02:14:43
【问题描述】:

基本上我有一个工厂

angular.module('app').factory('gService',gService);
function gService($filter, $window) {
    function confirmDialog(message, success, fail) {
        var confirmMessage = navigator.notification.confirm(
                message,
                onConfirm,
                '',
                [$filter('translate')('OK'), $filter('translate')('CANCEL')]
            );
        function onConfirm(index) {         
            return index === 1 ? success() : fail();
        }
        return confirmMessage;
    }
}

我想检查这个工厂外的情况,是否执行了功能

if(gService.confirmDialog.onConfirm){

}

这不起作用。如何检查以角度执行的函数?

【问题讨论】:

  • 那里的索引是什么??
  • 它来自 inbuit 函数。
  • 您是否在控制台中遇到任何错误?你在哪里写的if语句。?您必须在服务中返回该功能。我写了一个控制器并注入了服务。称为函数服务函数。它正在工作。
  • 它实际上是一个弹出窗口。我正在检查工厂外的情况,所以我使用gService.confirmDialog.onConfirm
  • @Matarishvan 您的问题不清楚。拜托,我请你 edit 并解释你想要实现的具体目标,我们需要更多的宏细节 - 你想只显示此消息一次然后检查用户是否确认检查是什么?您需要添加上下文。这将帮助我们为您提供更好的解决方案。谢谢

标签: javascript angularjs if-statement conditional-statements


【解决方案1】:

尝试以下方法:

angular.module('app').factory('gService', gService);
function gService($filter, $window) {
  var observers = [];
  function observeNotify(callback) {
    observers.push(callback);
  }
  function confirmDialog(message, success, fail) {
    var confirmMessage = navigator.notification.confirm(
      message,
      onConfirm,
      '',
      [$filter('translate')('OK'), $filter('translate')('CANCEL')],
    );
    function onConfirm(index) {
      var condition = index === 1;
      observers.forEach(function(observer) {
        observer(condition);
      });
      return condition ? success() : fail();
    }
    return confirmMessage;
  }
}
//Outside of the factory

gService.observeNotify(function(condition){
  console.log(condition);
});

这样你就可以注册函数,在onConfirm函数的执行过程中被调用

【讨论】:

    【解决方案2】:

    你为什么不这样做。

    angular.module('app').factory('gService',gService);
    function gService($filter, $window) {
        var obj = {
            confirmDialog : confirmDialog,
            onConfirm : onConfirm,
        }
        obj.executed = false;
        function confirmDialog(message, success, fail) {
            var confirmMessage = navigator.notification.confirm(
                    message,
                    onConfirm,
                    '',
                    [$filter('translate')('OK'), $filter('translate')('CANCEL')]
                );
            function onConfirm(index) {         
                if(index===1){
                    //I dont know you want function or returned value of function but you can use success() as well
                    obj.executed = success;
                    return success();
                }else{
                    obj.executed = fail;
                    return fail();
                }
            }
            return confirmMessage;
        }
        return obj;
    }
    

    你现在可以检查..

    如果(gService.executed){

    }

    我不知道你是否忘记了,但你还没有从工厂退回任何东西。

    【讨论】:

      【解决方案3】:

      EMIT & BROADCAST

      如果您检查onConfirm 事件,它将控制是onConfirm 函数,该函数定义在语句所写的gService.confirmDialog 对象上。这不是异步和承诺的工作。

      if(gService.confirmDialog.onConfirm){
      
      }
      

      您需要先通知您的听众。之后收听该事件来完成你的工作。

      您可以将broadcastemit 事件添加到等待onConfirm 事件的作用域

         angular.module('app').factory('gService',gService);
          function gService($rootScope, $filter, $window) {
              function confirmDialog(message, success, fail) {
                  var confirmMessage = navigator.notification.confirm(
                          message,
                          onConfirm,
                          '',
                          [$filter('translate')('OK'), $filter('translate')('CANCEL')]
                      );
                  function onConfirm(index) {
                      var result = index === 1 ? success() : fail();
                      $rootScope.$emit('onConfirm', result); 
                      //or
                      //$rootScope.$broadcast('onConfirm', result); -> this goes downwards to all child scopes. Emit is upwarded.
      
                  }
                  return confirmMessage;
              }
          }
      

      之后您应该检查是否触发了onConfirm 事件。这就是你需要的控制。

      function onConfirmFunction( result ){ //You will get the success or fail methods result here... };
      
      $rootScope.$on('onConfirm', onConfirmFunction);
      

      【讨论】:

        【解决方案4】:

        基本上你不会从函数gService(你的工厂的实现)返回(引用你共享的代码)任何东西,因为它是factory,你必须返回一些东西。你的内部函数confirmDialog 返回一个对象,但外部函数gServive 不返回任何东西。您想要执行它的方式,您必须返回 confirmDialog 回调并且必须在注入端执行,如

        if(gServive(<..args...>).onConfirm())

        如果玩具返回一个对象,那么

        return {confirmDialog: confirmDialog(...<args>...)}

        然后在注入端

        if(gService.confirmDialog.onConfirm)

        注意:你的 onConfirm 是一个函数,通过调用它,它会返回一些东西,所以它必须用(...) 调用,它实际上听起来像一个事件附加函数,它将进行回调并在完成时触发,但您的代码并没有这么说。在这种情况下,您可以维护一个承诺,并在onConfirm 调用中将回调作为参数,然后将其传递给该承诺的then,每当您完成确认时,只需解决该承诺。

        【讨论】:

          【解决方案5】:

          将 onComfirm 转换为 Promise,并在 Promise 解决后做一些事情。

          function onConfirm(index) {
            return $q(function (resolve, reject) {
            index === 1 ? resolve(true) : reject(false);
            });
           }
          
          
          
            $scope.callFun = function () {
              gService.confirmDialog("", "","").onConfirm()
                 .then(function(sucess) {
                    successMethod();  
                  }, function(error) {
                     console.error('oh no');
                  });
          

          【讨论】:

            【解决方案6】:

            这是您所期待的吗?我不确定是什么问题。能详细解释一下吗?

            <!doctype html>
            <html lang="en" ng-app="app">
            
            <head>
              <meta charset="utf-8">
              <title>How AngularJS Works?</title>
              <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js"></script>
            </head>
            
            <body ng-controller="appCtrl">
            
              <p>Welcome to BooKart, we have collection of {{1 + ' Million'}} books.</p>
              <script>
                var app = angular.module('app', []);
                app.factory('gService', gService);
            
                function gService($filter, $window) {
                  function confirmDialog(message, success, fail) {
                    alert("called");
                    var confirmMessage = navigator.notification.confirm(
                      message,
                      onConfirm,
                      '', [$filter('translate')('OK'), $filter('translate')('CANCEL')]
                    );
            
                    function onConfirm(index) {
                      return index === 1 ? success() : fail();
                    }
                    return confirmMessage;
                  }
                  return {
                    confirmDialog:confirmDialog
                  }
                }
            
                app.controller("appCtrl", function ($scope, gService) {
                  $scope.callFun = function () {
                    if (gService.confirmDialog("", "","").onConfirm) {
            
                    }
                  };
                  $scope.callFun();
                });
              </script>
            
            
            </body>
            
            </html>
            

            【讨论】:

            • 没有。实际上条件gService.confirmDialog.onConfirm我无法检查..这就是我要找的。​​span>
            • 这是您所期待的吗?我不确定是什么问题。能详细解释一下吗?那就不要回答了。相反,请在 cmets 中让 OP 澄清自己,如果您决定这样做,请将关闭标记为“不清楚您在问什么”。
            猜你喜欢
            • 1970-01-01
            • 2016-08-09
            • 2018-12-20
            • 2023-04-04
            • 2016-11-17
            • 2021-12-29
            • 2014-11-04
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多