错误:没有ValidateService的提供者

时间:2017-03-31 12:34:14

标签: angular service

我正在使用@angular/cli。通过创建名为services的文件夹验证我使用验证服务的表单,然后运行ng g service validate。 在validate.service内创建了两个文件,我写了一些验证函数。然后我imported将其constructor添加到我的另一个文件中,并在this内创建一个实例,使用 constructor(private vd : ValidateService) { } 关键字调用它。如下所示。

ng serve

然后我查看ERROR Error: Uncaught (in promise): Error: No provider for ValidateService! 。所以它给出了以下错误

@implementation Staffdir
@synthesize tableview,filteredContentList,searchBar1;
- (void)viewDidLoad {
    searchBar1.delegate = self;
         __block   StaffDirectoryModel*staffdirectorymodel;
        NSURL *url = [NSURL URLWithString:@"http://dev.devobal.com/GetData.aspx?Query=select%20*%20from%20tb_RHP_Staff_Directory"];
    _staffdirarr=[[NSMutableArray alloc]init];
    NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:url];

    [[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSMutableArray *jsonarr=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

        dispatch_sync(dispatch_get_main_queue(), ^{
            // Update UI
            for(int i=0;i<jsonarr.count;i++){
                staffdirectorymodel=[[StaffDirectoryModel alloc]init];
                staffdirectorymodel.name =[[jsonarr objectAtIndex:i]objectForKey:@"Name"];
                staffdirectorymodel.occupation=[[jsonarr objectAtIndex:i]objectForKey:@"Job_Title"];

                staffdirectorymodel.phonelabel =[[jsonarr objectAtIndex:i]objectForKey:@"Phone"];

                staffdirectorymodel.faxlabel =[[jsonarr objectAtIndex:i]objectForKey:@"Fax_Number"];
                 staffdirectorymodel.department =[[jsonarr objectAtIndex:i]objectForKey:@"Department"];
                [_staffdirarr addObject:staffdirectorymodel];
                staffdirectorymodel=nil;
            }
                       [tableview reloadData];

        });




    }]resume];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    return 1;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (isSearching) {
        return [searchResults count];
    }else{
        return _staffdirarr.count;
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellidentifer = @"cell";

    StaffDirectoryModel*staffdirectorymodel=[[StaffDirectoryModel alloc]init];
    Staffcustom *cell= [tableView dequeueReusableCellWithIdentifier:cellidentifer];

    if (cell == nil) {

        cell = [[Staffcustom alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifer];
    }
    [cell.fxlabel setHidden:NO];
    [cell.faxnumlabel setHidden:NO];
    if (isSearching) {

          staffdirectorymodel=[searchResults objectAtIndex:indexPath.row];


        cell.namelabel.text = staffdirectorymodel.name;
        cell.occupationlabel.text = staffdirectorymodel.occupation;

        NSString*tempph=staffdirectorymodel.phonelabel;
        NSString*tempfax=staffdirectorymodel.faxlabel;


        if(tempph.length!=0)
        {
            cell.phonelabel.text = staffdirectorymodel.phonelabel;
        }
        else{
            cell.phlabel.text=@"Fax: +";
            cell.phonelabel.text = staffdirectorymodel.faxlabel;
            [cell.fxlabel setHidden:YES];
            [cell.faxnumlabel setHidden:YES];

        }


        if(tempfax.length==0)
        {
            [cell.faxnumlabel setHidden:YES];
            [cell.fxlabel setHidden:YES];

        }

        if([tempph length]!=0 &&[tempfax length]!=0){
            cell.faxnumlabel.text = staffdirectorymodel.faxlabel;
        }



        cell.Depart.text = staffdirectorymodel.department;






    }
    else{

        staffdirectorymodel=[_staffdirarr objectAtIndex:indexPath.row];


        cell.namelabel.text = staffdirectorymodel.name;
    cell.occupationlabel.text = staffdirectorymodel.occupation;
        NSString*tempph=staffdirectorymodel.phonelabel;
        NSString*tempfax=staffdirectorymodel.faxlabel;
        if(tempph.length!=0)
        {
        cell.phonelabel.text = staffdirectorymodel.phonelabel;
        }
        else{
            cell.phlabel.text=@"Fax: +";
            cell.phonelabel.text = staffdirectorymodel.faxlabel;
            [cell.fxlabel setHidden:YES];
            [cell.faxnumlabel setHidden:YES];

        }

        if(tempfax.length==0)
        {
                [cell.faxnumlabel setHidden:YES];
                [cell.fxlabel setHidden:YES];

        }

        if([tempph length]!=0 &&[tempfax length]!=0){
    cell.faxnumlabel.text = staffdirectorymodel.faxlabel;
        }
    cell.Depart.text = staffdirectorymodel.department;

    }
     cell.namelabel.textColor = [UIColor blueColor];
    cell.occupationlabel.textColor=[UIColor colorWithRed:0.5 green:0.0 blue:1 alpha:1.0];

    UIView * additionalSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,cell.frame.size.height-1,cell.frame.size.width,0.1)];
    additionalSeparator.backgroundColor = [UIColor blueColor];
    [cell addSubview:additionalSeparator];
        return cell;

}
//- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
//    isSearching = YES;
//}
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText
{
    //[filteredContentList removeAllObjects];

    if(searchText.length!=0){
        isSearching=YES;
        NSPredicate *resultPredicate = [NSPredicate
                                        predicateWithFormat:@"name contains[c] %@",
                                        searchText];

        searchResults=(NSMutableArray *)[_staffdirarr filteredArrayUsingPredicate:resultPredicate];
        [tableview reloadData];
    }
    else{
        isSearching=NO;
        [tableview reloadData];
    }

    //
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    isSearching=YES;
    [tableview reloadData];}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    isSearching=NO;
    [tableview reloadData];}

有人说,这个捆绑包坏了,它很快就会解决,而不是使用dev bundle。这是什么。我不知道

2 个答案:

答案 0 :(得分:2)

在app.module.ts文件中使用Provider,如

@NgModule({
    imports: [],
 providers: [ValidateService]
})

答案 1 :(得分:2)

有两种方法可以做到这一点和最新的做法:

首先:

@NgModule({
    imports: [],
 providers: [ValidateService]
})

第二

@Component({
    selector: 'countries',
    providers: [ValidateService]
})

这取决于要求。

  

如果您希望每个组件实例有一个服务实例,请将其添加到   组件的提供者。如果你想要一个独特的实例   整个应用程序,将其添加到NgModule的提供程序。这包括在内   有关服务和依赖注入的文档。

     

@JB Nizet在评论部分

中解释