Angular JS应用服务
Angular JS Applying Service
根据最佳实践,我正在尝试将我的全局函数包装到可重用的工厂服务中。在下面的代码中,我尝试运行一个函数,该函数将在我的 JSON 数据中获取"Qprogress"的字符串值,对其运行一些数学运算并将输出附加到具有"百分比"类的元素。我怎样才能做到这一点?
.HTML:
<table class="table table-striped table-condensed" ng-controller = "clientStatus">
<thead>
<tr>
<th>Client</th>
<th>Status</th>
<th>Icon</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in clients">
<td><a href="#/details/{{clients.indexOf(item)}}" title="Link to {{item.FirstName}} {{item.LastName}}" class="oly-open">{{item.FirstName}} {{item.LastName}}</a></td>
<td ng-hide="item.Progress == 'In Progress'" ng-class="{ 'status-success': item.Progress == 'Questionnaire Completed', 'status-error': item.Progress == 'Unsent'}">{{item.Progress}}</td>
<td ng-if="item.Progress == 'In Progress'" class="status-info percent" ng-init="progressCalc(item)"></td>
<td width="10%"><a href="#/reports/" title{{$index + 1}}="Reports" ng-show="{{item.Progress == 'Questionnaire Completed'}}"><span class="stat-icon-report"></span></a> <a href="#/sends/{{$index + 1}}" title="Alert" ng-show="{{item.Progress == 'Unsent'}}"><span class="stat-icon-bullhorn"></span></a> <a href="#/progress/{{$index + 1}}" title="In Progress" ng-show="{{item.Progress == 'In Progress'}}"><span class="stat-icon-phone"></span></a></td>
</tr>
</tbody>
</table>
AngularJS:
var myApp = angular.module('myApp', []);
myApp.factory('progressCalc', function() {
angular.forEach(function(item) {
var m = 0.26664;
var s = 0.26664;
var i = 0.694375;
var t = item.Qprogress;
t = t.replace(/m/g, '');
t = t.replace(/s/g, '');
t = t.replace(/i/g, '');
var ta = t.split("/");
var tTotal = (ta[0] * m) + (ta[1] * s) + (ta[2] * i);
Math.round(tTotal);
(tTotal + '%').append('.percent');
});
});
myApp.controller('clientStatus', ['$scope', '$http', function($scope, $http) {
$http.get('assets/js/lib/angular/clientList.json').success(function(data) {
$scope.clients = data;
});
}]);
JSON的片段:
{
"FirstName": "Jane",
"LastName": "Greenberg",
"Company": "Nike",
"CompanyId": "2345672",
"ClientId": "EFTGE6",
"Title": "CIO",
"Phone": "555-555-5555",
"ClientSystemStatus": "Active",
"CreationDate": "06/12/2015, 9:12:27 am",
"Progress": "In Progress",
"Qprogress": "m125/s108/i0",
"Email": "jgreenberg@nike.com"
}
我以为我可以将我的代码包装在一个函数中,然后在我附加到的元素上进行 ng-init,但这不想工作。
您的factory
应该返回可重用的部分,该部分将在controller
中使用。计算发生在factory
,而控制器只调用它并传递给 DOM(在您的情况下,它应该是一个更好的目录(。
//we define the factory, it will return on method 'doMath'
myApp.factory('progressCalc', function() {
return {
doMath: function(data) {
//here we will store the results
var values = [];
angular.forEach(function(data) {
var m = 0.26664;
var s = 0.26664;
var i = 0.694375;
var t = item.Qprogress;
t = t.replace(/m/g,'');
t = t.replace(/s/g,'');
t = t.replace(/i/g,'');
var ta = t.split("/");
var tTotal = (ta[0] * m) + (ta[1] * s) + (ta[2] * i);
Math.round(tTotal);
values.push(tTotal + '%');
});
//here give the results back to controller
return values;
}
}
});
//your controller
myApp.controller('clientStatus', ['$scope', '$http', 'progressCalc', function($scope, $http, progressCalc) {
//...other stuff
$http.get('assets/js/lib/angular/clientList.json').success(function(data) {
//here we have the data
//and call progressCalc factory
$scope.clients = progressCalc.doMath(data);
//here you can do smth like
$scope.clients.forEach(function(item){
item.append('.percent')
});
});
});
同样在您的函数中,您将需要进行一些更改(请参阅第一个答案(,可能
angular.forEach(function(data) {
应该是
angular.forEach(data, function(item) {
...
tTotal = Math.round(tTotal);
...
相关文章:
- Windows 8市场应用程序JS,访问远程XML文件
- 节点中的端点与快速应用程序.js之间的冲突
- 应用程序.js未在开发中加载(未捕获的引用错误:未定义 $)
- 有没有一种方法可以在IE8中解决我的Rails javascript应用程序.js的问题
- iOS在safari中打开URL,而不是在应用浏览器JS、HTML中打开Facebook
- 如何获取应用程序.js其中不包括压缩代码
- 在动态生成的内容上应用Shadowbox.js
- 设置应用程序.js使用 git bash 错误
- 带帆的应用程序.js没有缓存浏览器
- 在应用程序.js中访问 io 对象
- 反应.js应用程序.js文件大小
- Windows商店应用程序JS:使用WinJS.xhr上传视频文件
- 加载应用程序.js在设备中准备好煎茶触摸科尔多瓦应用程序
- 多变量是节点应用程序.js
- 应用程序.js在部分内不起作用
- Angular JS应用服务
- 不知道如何在节点中运行应用程序.js
- 节点应用程序.js如何使用其推荐的功能
- 应用程序.js在骨干中不起作用.js + 要求.js
- 更改服务.js中的变量值