Javascript 两个控制器中的一个功能[JS]
这里是我的问题:我这里有一个小JS程序,其中有两个控制器。。我的问题是两个控制器中都需要一个函数(data.duration)。因为有一次我需要功能本身的函数,还有一次需要摘要。顺便说一句,我通过一个JSON报告获取信息,函数会证明它是一个特性还是一个值。当函数在代码中出现2次时 那么,有没有办法在两个控制器中都使用该函数Javascript 两个控制器中的一个功能[JS],javascript,json,angularjs,Javascript,Json,Angularjs,这里是我的问题:我这里有一个小JS程序,其中有两个控制器。。我的问题是两个控制器中都需要一个函数(data.duration)。因为有一次我需要功能本身的函数,还有一次需要摘要。顺便说一句,我通过一个JSON报告获取信息,函数会证明它是一个特性还是一个值。当函数在代码中出现2次时 那么,有没有办法在两个控制器中都使用该函数 (function() { var loader = ['$http', function($http) {
(function() {
var loader = ['$http',
function($http) {
return $http.get('report.json')
.success(function(data) {
function getFailedScenarioCount(feature) {
var failedScenarios = 0;
feature.scenarios.forEach(function(scenario) {
if (scenario.result.failedStepCount)
failedScenarios++;
});
return failedScenarios;
}
function getUnknownScenarioCount(feature) {
var unknownScenarios = 0;
feature.scenarios.forEach(function(scenario) {
if (scenario.result.unknownStepCount)
unknownScenarios++;
});
return unknownScenarios;
}
angular.forEach(data.features, function(feature) {
if (feature.scenarios.length) {
feature.result.failedScenarioCount = getFailedScenarioCount(feature);
feature.result.unknownScenarioCount = getUnknownScenarioCount(feature);
feature.result.passedScenarioCount = feature.result.scenarioCount - feature.result.failedScenarioCount - feature.result.unknownScenarioCount;
if (feature.result.failedScenarioCount === 0)
feature.result.failedScenarioCount = null;
if (feature.result.unknownScenarioCount === 0)
feature.result.unknownScenarioCount = null;
if (feature.result.passedScenarioCount === 0)
feature.result.passedScenarioCount = null;
}
feature.status = feature.result.failedScenarioCount ? "FAILED" : "OK";
angular.forEach(feature.scenarios, function(scenario) {
angular.forEach(scenario.steps, function(step) {
if (step.result.status === "undefined")
step.result.status = "unknown";
});
scenario.status = scenario.result.failedStepCount ? "failed" : (scenario.result.unknownStepCount ? 'unknown' : 'passed');
});
});
data.duration = function(feature) {
var day = 0;
var h = 0;
var min = 0;
var sec = 0;
var value = 0;
var substract = function(v, b) {
var result = v - (b);
return result;
};
if (isNaN(feature)) {
value = feature.result.duration;
} else {
value = feature;
}
console.log("value: " + value);
if (value % (1000000000 * 60 * 60 * 24) >= 0) //day
{
day = (value / (1000000000 * 60 * 60 * 24)) | 0;
value = substract(value, (day * (1000000000 * 60 * 60 * 24)));
}
if (value % (1000000000 * 60 * 60) >= 0) //hour
{
h = (value / (1000000000 * 60 * 60)) | 0;
value = substract(value, (h * (1000000000 * 60 * 60)));
if (h % 24 === 0 && h !== 0) {
day++;
h = 0;
}
}
if (value % (1000000000 * 60) >= 0) //minute
{
min = (value / (1000000000 * 60)) | 0;
value = substract(value, (min * (1000000000 * 60)));
if (min % 60 === 0 && min !== 0) {
h++;
min = 0;
}
}
if (value % (1000000000) >= 0) //second
{
sec = (value / (1000000000)) | 0;
value = substract(value, (sec * (1000000000)));
if (sec % 60 === 0 && sec !== 0) {
min++;
sec = 0;
}
}
if (day === 0 && h === 0 && min === 0 && sec === 0) {
var msec = 0;
if (value % 1000000 >= 0) {
msec = (value / 1000000) | 0;
}
return msec > 0 ? msec + 'ms' : '<1ms';
} else if (day > 0) {
return (' ' + '(' + day + 'Day/s) ' + ' ' + ' ' + h + ' ' + ':' + ' ' + min + ' ' + ':' + ' ' + sec);
} else {
return (' ' + h + ' ' + ':' + ' ' + min + ' ' + ':' + ' ' + sec);
}
};
});
}
];
var app = window.angular.module('cucumber', ['ui.bootstrap']);
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.when('/features', {
templateUrl: 'features.html',
controller: 'FeatureListCtrl',
resolve: {
report: loader
}
}).when('/feature/:featureId', {
templateUrl: 'feature.html',
controller: 'FeatureCtrl',
resolve: {
report: loader
}
}).otherwise({
redirectTo: '/features'
});
}
]);
app.controller('FeatureListCtrl', function($rootScope, $scope, $location, $filter, report) {
$scope.report = report.data;
$rootScope.reportDate = $scope.report.date;
$scope.duration = $scope.report.duration;
$scope.featureDetails = function(feature) {
$location.path('/feature/' + feature.id);
};
$scope.sum = function(features, field) {
if (!features) return null;
var sum = features.reduce(function(sum, feature) {
var value = parseInt(feature.result[field], 10);
return isNaN(value) ? sum : sum + value;
}, 0);
return sum > 0 ? sum : null;
};
$scope.$watch("searchText", function(query) {
$scope.filteredFeatures = $filter("filter")($scope.report.features, query);
});
$scope.isCollapsed = true;
});
app.controller('FeatureCtrl', function($rootScope, $scope, $location, $routeParams, report) {
$scope.duration = report.data.duration;
function getFeature(featureId, features) {
for (var i = 0; i < features.length; i++) {
if (featureId === features[i].id) {
return features[i];
}
}
}
$scope.feature = getFeature($routeParams.featureId, report.data.features);
$rootScope.reportDate = report.data.date;
});
}());
(函数(){
变量加载器=['$http',
函数($http){
返回$http.get('report.json')
.成功(功能(数据){
函数getFailedScenarioCount(功能){
var failedScenarios=0;
feature.scenarios.forEach(函数(场景){
if(scenario.result.failedStepCount)
失败的场景++;
});
返回失败的场景;
}
函数getUnknownScenarioCount(功能){
var unknownScenarios=0;
feature.scenarios.forEach(函数(场景){
if(scenario.result.unknownStepCount)
unknownScenarios++;
});
返回未知事件;
}
角度.forEach(数据.特征,函数(特征){
if(feature.scenarios.length){
feature.result.failedScenarioCount=getFailedScenarioCount(特征);
feature.result.unknownScenarioCount=getUnknownScenarioCount(特征);
feature.result.passedScenarioCount=feature.result.scenarioCount-feature.result.failedScenarioCount-feature.result.unknownScenarioCount;
如果(feature.result.failedScenarioCount==0)
feature.result.failedScenarioCount=null;
if(feature.result.unknownScenarioCount==0)
feature.result.unknownScenarioCount=null;
if(feature.result.passedScenarioCount==0)
feature.result.passedScenarioCount=null;
}
feature.status=feature.result.failedScenarioCount?“失败”:“确定”;
angular.forEach(功能.场景,功能(场景){
angular.forEach(场景.步骤,函数(步骤){
如果(step.result.status==“未定义”)
步骤.result.status=“未知”;
});
scenario.status=scenario.result.failedStepCount?“失败”:(scenario.result.unknownStepCount?“未知”:“已通过”);
});
});
data.duration=功能(特性){
var日=0;
var h=0;
var min=0;
var-sec=0;
var值=0;
var减记=函数(v,b){
var结果=v-(b);
返回结果;
};
如果(isNaN(功能)){
值=feature.result.duration;
}否则{
价值=特征;
}
console.log(“值:”+value);
如果(值%(100000000*60*60*24)>=0)//天
{
日=(值/(100000000*60*60*24))| 0;
值=减法(值,(天*(100000000*60*60*24));
}
如果(值%(100000000*60*60)>=0)//小时
{
h=(值/(100000000*60*60))|0;
值=减法(值,(h*(100000000*60*60));
如果(h%24==0&&h!==0){
day++;
h=0;
}
}
如果(值%(100000000*60)>=0)//分钟
{
最小值=(值/(100000000*60))| 0;
值=减法(值,(最小值*(100000000*60));
如果(最小值%60==0&&min!==0){
h++;
最小值=0;
}
}
如果(值%(100000000)>=0)//秒
{
sec=(值/(100000000))| 0;
值=减法(值,秒*(100000000));
如果(秒%60==0&&sec!==0){
min++;
秒=0;
}
}
如果(日===0&&h==0&&min==0&&sec==0){
var-msec=0;
如果(值%1000000>=0){
msec=(值/1000000)| 0;
}
返回msec>0?msec+'ms':'非常简单,编写一个服务并将其注入两个控制器
app.service("utilityService", function () {
return {
duration: function (feature) {
...your code
}
}
});
然后将其注入每个控制器:
app.controller('FeatureListCtrl', function($rootScope, $scope, $location, $filter, report, utilityService) {
});
然后参考它:
utilityService.duration($scope.whatEverData)