我有3个执行类似任务的控制器:
> PastController查询API以查找过去的系统中断.
> CurrentController查询API以查找当前系统中断
> FutureController查询API以确定将来的系统中断
它们各有特色(尽管它们具有相似的功能).但是,它们都是从定义相同的$scope变量开始的:
app.controller("PastController",function ($scope) { $scope.Outages = ""; $scope.loading = 0; $scope.nothing = 0; $scope.error = 0; //--- code continues ---// }); app.controller("CurrentController",function ($scope) { $scope.Outages = ""; $scope.loading = 0; $scope.nothing = 0; $scope.error = 0; //--- code continues ---// }); app.controller("FutureController",function ($scope) { $scope.Outages = ""; $scope.loading = 0; $scope.nothing = 0; $scope.error = 0; //--- code continues ---// });
我没有测试代码,但如果你想使用服务,这是我的想法,希望它有效.
首先创建服务:
app.service('systemService',function(){ // initialize object first this.info = {}; this.initialize = function(){ // properties initialization this.info.Outages = ""; this.info.loading = 0; this.info.nothing = 0; this.info.error = 0; return this.info; } this.fooFunction = function() { return "Hello!" }; });
最后,您必须正确地将创建的服务注入控制器并从服务调用initialize函数:
app.controller("PastController",['$scope','systemService',function ($scope,systemService) { $scope.info = systemService.initialize(); $scope.fooFunction = systemService.fooFunction(); }]);
…并在每个控制器中设置.
原文地址:https://www.jb51.cc/angularjs/143383.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。