我有两个不同的 HTML 文件。选择按钮存在于两个 html 文件中。如果我点击那个按钮,我需要选择卡片。
我有两个功能。第一个是默认情况下我需要在屏幕上显示。
第二个是我需要在弹出模式中显示相同的数据。
- home.html
<button type="button" class="btn btn-primary col-xs-12 col-md-12 col-lg-12"
ng-click="selectCard()">ADD CARD</button>
- page.html
<button type="button" class="btn btn-primary col-xs-12 col-md-12 col-lg-12"
ng-click="selectCard()">ADD CARD</button>
这些都是相同的 html 代码。
- 主 Controller .js
$scope.selectCard = function () {
$scope.select = true;
}
但我的问题是,
页。 html 文件处于弹出模式。
$scope.view = function () {
$uibModal.open({
templateUrl: 'page.html',
size: 'lg',
windowClass: 'page',
controller: function ($scope, $uibModalInstance, $timeout) {
$scope.selectCard = function(){
$scope.select = true;
}
}
});
}
在弹出模式中,我也需要再次编写相同的函数。然后只有它在工作。
有什么方法可以在两个地方使用相同的功能。
最佳答案
对于常用的功能,服务是最好的地方。您应该根据 Controller 的要求将您的服务添加为依赖注入(inject)。
要创建和注入(inject)服务,您可以引用这个官方 angularjs 文档站点: https://docs.angularjs.org/guide/services
关于javascript - 如何从angularjs中的两个不同地方调用同一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58029430/