gpt4 book ai didi

javascript - 以 Angular 将服务添加到 Controller

转载 作者:行者123 更新时间:2023-12-03 11:33:16 26 4
gpt4 key购买 nike

我尝试构建一个返回格式化日期的服务,当我将其放入 Controller 中时,这是有效的,但我在很多地方都需要它,所以我想我应该构建一个服务。现在,当我尝试将服务注入(inject) Controller 时,它给了我一个未知的提供程序问题..

Unknown provider: dateProvider <- date <- displaydate

当前代码:

服务.js

angular.module('starter.services', [])
.factory('displaydate',['date','$filter', function(date, $filter) {
var actiondate = new Date(date);
var today = new Date();
if(today.getDate() == actiondate.getDate()){
var hourssince = today.getHours() - actiondate.getHours()
var minutessince = today.getMinutes() - actiondate.getMinutes()
var secondssince = today.getSeconds() - actiondate.getSeconds()
if(hourssince > 0){
date = hourssince+'u';
}else if(minutessince > 0){
date = minutessince+'m';
}else{
date = secondssince+'s';
}
}else{
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var diffDays = Math.round(Math.abs((today.getTime() - actiondate.getTime())/(oneDay)));
if(diffDays > 28){
var identifier = actiondate.getMonth();
date = $filter('date')(actiondate,"d "+ maandarray[identifier] + " yy " + " HH:" + "mm");
}else{
date = diffDays+'d';
}
}
return date;
}]);

Controller .js

angular.module('starter.controllers', ['google-maps'.ns(),'starter.services'])
.controller('VolgendCtrl', function($scope, displaydate) {
var date = displaydate(value[4]);
})

应用程序.js

var starter = angular.module('starter', ['ionic','starter.services',  'starter.controllers' ,'ngCordova'])

starter.config(function($stateProvider, $urlRouterProvider,$httpProvider, $ionicConfigProvider) {
$ionicConfigProvider.prefetchTemplates(true);
$urlRouterProvider.otherwise('/tab/volgend');
$stateProvider

// setup an abstract state for the tabs directive
.state('tab', {
url: "/tab",
abstract: true,
templateUrl: "templates/tabs.html"
})

.state('tab.volgend', {
url: '/volgend',
views: {
'volgend': {
templateUrl: 'templates/volgend.html',
controller: 'VolgendCtrl',
}
}
})
// HOME STATES AND NESTED VIEWS ========================================
});

starter.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
ionic.Platform.isFullScreen = true
});
})

有人可以向我解释为什么它没有像我预期的那样工作吗?

最佳答案

如果“日期”,您在 displayDate 中注入(inject)的内容实际上是您在 Controller 中尝试调用 displayDate 的内容,而不是其他服务,那么您不应该按照您的方式注入(inject)它。然后你应该只注入(inject) $filter ,然后从工厂函数返回一个带有“date”参数的函数,并在该函数中进行计算。

angular.module('starter.services', [])
.factory('displaydate',['$filter', function($filter) {
return function (date){
var actiondate = new Date(date);
var today = new Date();
if(today.getDate() == actiondate.getDate()){
var hourssince = today.getHours() - actiondate.getHours()
var minutessince = today.getMinutes() - actiondate.getMinutes()
var secondssince = today.getSeconds() - actiondate.getSeconds()
if(hourssince > 0){
date = hourssince+'u';
}else if(minutessince > 0){
date = minutessince+'m';
}else{
date = secondssince+'s';
}
}else{
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var diffDays = Math.round(Math.abs((today.getTime() - actiondate.getTime())/(oneDay)));
if(diffDays > 28){
var identifier = actiondate.getMonth();
date = $filter('date')(actiondate,"d "+ maandarray[identifier] + " yy " + " HH:" + "mm");
}else{
date = diffDays+'d';
}
}
return date;
}
}]);

关于javascript - 以 Angular 将服务添加到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26651310/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com