ngx-bootstrap icon indicating copy to clipboard operation
ngx-bootstrap copied to clipboard

not-issue(forms): No value accessor for ''

Open ghost opened this issue 10 years ago • 12 comments

Hi guys,

I get this error when I add a pagination component to my Angular 2 LocationListComponent:

EXCEPTION: Error: Uncaught (in promise): Template parse errors:
No provider for NgModel ("
        <div class="col-md-10">

            [ERROR ->]<pagination [totalItems]="totalItems" [(ngModel)]="currentPage" (pageChanged)="pageChanged($event)"><"): LocationListComponent@96:12

I've imported all required dependencies (CORE_DIRECTIVES and FORM_DIRECTIVES) following the official documentation page at http://valor-software.com/ng2-bootstrap/#/pagination for TypeScript and HTML template

import {CORE_DIRECTIVES} from "@angular/common";
import {FORM_DIRECTIVES} from "@angular/forms";
import {PAGINATION_DIRECTIVES} from "ng2-bootstrap/ng2-bootstrap";]

and

<pagination [totalItems]="totalItems" [(ngModel)]="currentPage (pageChanged)="pageChanged($event)"></pagination>

Anyone has any idea why this happens?

  • I use Angular 2 RC.4
  • I use a old "form" system
  • I use a new "router" system "3.0.0-beta.2"
  • I use nested routes (more than 1 level) in this context

Is it probably a bug in the latest release (1.0.23)?

Thank you

ghost avatar Jul 18 '16 14:07 ghost

An update

If I use a new form library (0.2.0) I get this error:

EXCEPTION: Error in app/location/list.html:96:50
ORIGINAL EXCEPTION: No value accessor for ''

where at line 96 of list.html there is the HTML pagination invocation:

<pagination [totalItems]="totalItems" [(ngModel)]="currentPage" (pageChanged)="pageChanged($event)"></pagination>

Probably the bug is the same, but I don't understand "why". Thank you

ghost avatar Jul 18 '16 14:07 ghost

almost there: please add this 2 lines https://github.com/valor-software/ng2-bootstrap/blob/development/demo/index.ts#L57-L58

valorkin avatar Jul 19 '16 06:07 valorkin

Hi valorkin,

thank you for your quickly reply; I've already added both lines in bootstrap AppComponent. This exception it's appeared after upgrade from 1.0.17 to 1.0.23.

Thank you

ghost avatar Jul 19 '16 07:07 ghost

I use a old "form" system

unfortunately module can not support both forms so I had to move to new one >.< so you will have too

valorkin avatar Jul 19 '16 07:07 valorkin

Oh, great, I understand the issue: I need to remove all "old forms" imports from my component (even parent and children components).

Now works fine, thank you for support, great library!

:+1:

ghost avatar Jul 19 '16 07:07 ghost

Thanks :) doing our best :)

valorkin avatar Jul 19 '16 07:07 valorkin

Hi guys,

I am experiencing the same issue as described originally on 1.0.24.

I use the new forms API (followed this http://stackoverflow.com/questions/37871271/how-to-migrate-angular-2-rc-1-or-earlier-forms-to-angular-2-rc-2-rc-4-new-fo/37902014#37902014 ), so there is no clash with old forms.

I use the model driven forms binding so that I can use custom validators, which is the only difference between the example pagination and my implementation.

I also stumbled upon this stack overflow http://stackoverflow.com/questions/34995938/ngmodel-custom-valuesaccessor and noticed that Pagination component also implements ControlValueAccessor. In this tutorial http://restlet.com/blog/2016/02/17/implementing-angular2-forms-beyond-basics-part-2/ I saw that we need to define a special provider for ngModel binding to work.

So I copied the pagination.component.ts from ng2-bootstrap and added the following:

import { Component, OnInit, Input, Output, ElementRef, EventEmitter, Self, Renderer, forwardRef, Provider } from '@angular/core'; import {NgFor, NgIf, NG_VALUE_ACCESSOR} from '@angular/common'; import {ControlValueAccessor, NgModel, REACTIVE_FORM_DIRECTIVES} from '@angular/forms';

And then defined provider:

const PAGINATION_ACCESSOR = new Provider(
  NG_VALUE_ACCESSOR, {useExisting: forwardRef(() => PaginationComponent), multi: true});

/* tslint:disable */
@Component({
  selector: 'pagination[ngModel]',
  template: PAGINATION_TEMPLATE,
  directives: [NgFor, NgIf, REACTIVE_FORM_DIRECTIVES],
  providers: [PAGINATION_ACCESSOR]
})

And that seemed to fix the issue.

I am not that experienced with Angular2 so not sure if this fix is legitimate or not, could you please feedback on this?

Thank you in advance for help, and thank you for the great library!

P.S. I have the two lines to disabled deprecated form in my main

denyspavlov avatar Jul 24 '16 18:07 denyspavlov

yeap, you simply hacked value accessor provider :) there are a simpler form of provider: {provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => PaginationComponent), multi: true}

valorkin avatar Jul 24 '16 18:07 valorkin

so is this something wrong with my project setup that I need to do this hack? or is this a legitimate hack that is to be included in ng2-bootstrap?

denyspavlov avatar Jul 24 '16 21:07 denyspavlov

Are you using latest ng2-bootstrap, angular 2? Are you using @angular/forms? Importing ngModel or form directives from it?

valorkin avatar Jul 24 '16 22:07 valorkin

We are using https://github.com/mgechev/angular2-seed as base for our project

Here is my package.json

 "dependencies": {
    "@angular/common": "2.0.0-rc.4",
    "@angular/compiler": "2.0.0-rc.4",
    "@angular/core": "2.0.0-rc.4",
    "@angular/forms": "0.2.0",
    "@angular/http": "2.0.0-rc.4",
    "@angular/platform-browser": "2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "2.0.0-rc.4",
    "@angular/router": "3.0.0-beta.2",
    "es6-module-loader": "^0.17.8",
    "core-js": "^2.4.0",
    "rxjs": "5.0.0-beta.6",
    "systemjs": "0.19.27",
    "zone.js": "^0.6.12",
    "ng2-bootstrap": "1.0.24",
    "reflect-metadata": "0.1.3",
    "angular2-in-memory-web-api": "0.0.14"
  }

And here is the component that includes pagination:

import {Component, OnInit, OnDestroy, OnChanges, Input} from '@angular/core';
import {NgIf, NgFor, CORE_DIRECTIVES } from '@angular/common';
import {FormBuilder, Validators, REACTIVE_FORM_DIRECTIVES} from '@angular/forms';
import {PAGINATION_DIRECTIVES} from 'ng2-bootstrap/ng2-bootstrap';
import {ShopVO, ShopUrlVO, UrlVO} from './../../shared/model/index';
import {ShopService, ShopEventBus, Util} from './../../shared/services/index';
import {DataControlComponent} from './../../shared/sidebar/index';
import {ModalComponent, ModalResult, ModalAction} from './../../shared/modal/index';
import {YcValidators} from './../../shared/validation/validators';

@Component({
  selector: 'shop-url',
  moduleId: module.id,
  templateUrl: 'shop-url.component.html',
  directives: [DataControlComponent, PAGINATION_DIRECTIVES, REACTIVE_FORM_DIRECTIVES, CORE_DIRECTIVES, ModalComponent]
})

export class ShopUrlComponent implements OnInit, OnChanges {

My main.ts

bootstrap(AppComponent, [
  disableDeprecatedForms(),
  provideForms(),
  APP_ROUTER_PROVIDERS,
  {
    provide: APP_BASE_HREF,
    useValue: '<%= APP_BASE %>'
  }
]);

Hope this helps.

denyspavlov avatar Jul 24 '16 22:07 denyspavlov

+1 to rewrite ngModel stuff

valorkin avatar Jul 25 '16 06:07 valorkin