简体   繁体   English

如何从 angularjs 的两个不同位置调用相同的 function

[英]how to call the same function from two different places in angularjs

I have two different HTML files.我有两个不同的 HTML 文件。 Select Button is there in two html files. Select 按钮位于两个 html 文件中。 If I click that button I need to select the card.如果我单击该按钮,我需要 select 卡。

  • I have two functions.我有两个功能。 First one is by default i need to show in screen.第一个默认情况下我需要在屏幕上显示。

  • Second one is I need to show the same data in a popup modal.第二个是我需要在弹出模式中显示相同的数据。

    1. home.html主页.html
 <button type="button" class="btn btn-primary col-xs-12 col-md-12 col-lg-12"
         ng-click="selectCard()">ADD CARD</button>
  1. page.html页.html
<button type="button" class="btn btn-primary col-xs-12 col-md-12 col-lg-12" 
        ng-click="selectCard()">ADD CARD</button>

These both are same html code.这些都是相同的 html 代码。

  1. mainController.js主控制器.js
$scope.selectCard = function () {
    $scope.select = true;               
}

But my problem is,但我的问题是,

page.页。 html file is in popup modal. html 文件处于弹出模式。

$scope.view = function () {
    $uibModal.open({
        templateUrl: 'page.html',
        size: 'lg',
        windowClass: 'page',
        controller: function ($scope, $uibModalInstance, $timeout) {          
          $scope.selectCard = function(){             
              $scope.select = true;
          }
        }
    });
}

Here inside popup modal also I need to write the same function again.在弹出模式中,我还需要再次编写相同的 function 。 Then only it is working.然后只有它在工作。

Is any way is there to use the same function in both places.有什么方法可以在两个地方使用相同的 function。

For common functions, services are the best places.对于常用功能,服务是最好的地方。 You should add your service as Dependency Injection as required in your controllers.您应该根据控制器中的要求将您的服务添加为依赖注入。

To create and inject service you can refer to this official angularjs documentation site: https://docs.angularjs.org/guide/services要创建和注入服务,您可以参考这个官方 angularjs 文档站点: https://docs.angularjs.org/guide/services

You can move the function to separate service and inject it in all places you need.您可以将 function 移动到单独的服务中,并将其注入您需要的所有地方。

In case it should be bound to mainController.js , service function can use rxJs Subject or just emit event to update value in mainController.js .如果它应该绑定到mainController.js ,服务 function 可以使用rxJs Subject或只是发出事件来更新mainController.js中的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中使用不同的参数从不同的地方调用相同的函数,并且仍然保留该函数的变量的旧值? - How to call same function from different places using different parameters in javascript and still retain old values of variables of that function? 如何从不同的地方调用两个onClick事件 - How to call two onClick events from different places React App 在两个地方使用相同的 function 但 API 调用不同。 我如何强制它每次都使用root? - React App Using the same function in two places but the API call is different. How do I force it to use the root everytime? 当函数的参数来自不同地方时如何调用函数 - How to call a function when its arguments come from different places 如何以两种不同的方式调用同一函数? - How to call the same function in two different ways? 如何从其他控制器AngularJS调用控制器中的函数 - How to call a function in a controller from a different controller AngularJS 在AngularJS的两个地方使用相同的控制器 - Using same controller in two places in AngularJS 如何链接来自不同控制器/位置的AngularJS Promise? - How to chain AngularJS promises coming from different controllers / places? 来自不同控制器的angularJs调用函数 - angularJs call function from different controllers 如何从同一对象中的不同函数调用对象定义的函数? - How to call a object defined function from the a different function in the same object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM