【发布时间】:2017-10-08 15:46:27
【问题描述】:
我有两个页面,其中有 1 个包含一些功能的通用选项卡。我已经为 1 页的该选项卡准备好代码,现在我想在第二页中重用所有这些代码,而无需复制代码。
例如,这是我的标签页 1 的代码:
app.controller('myCtrl', ['$scope', '$window', 'myService', '$filter', function ($scope, $window, myService,$filter) {
$scope.myObj = [
{id:1,location : null},
{id:2,location : null}
]
//Lots of other variables here which are common and will be used in both the tabs and lots of methods which are also common
}]);
$scope.myObj 在两个选项卡中通用的所有方法中都大量使用,因此我希望有 1 个通用 js 文件,我将在其中保留所有这些通用变量和方法,这样我就不必将所有这些方法复制粘贴到两个页面的控制器中以避免代码重复。
我找到了 1 个如下示例,但我不知道如何共享方法和复杂变量,例如我的 $scope.myObj:
【问题讨论】:
-
您可以使用服务和工厂来实现代码的重用性。参考此链接希望对您有所帮助:blog.thoughtram.io/angular/2015/07/07/…
-
@Learning 接受答案
标签: javascript angularjs