PeterStaev / NativeScript-Drop-Down

A NativeScript DropDown widget.
Apache License 2.0
105 stars 65 forks source link

Problems with isEnabled and ngModel #152

Closed wendt88 closed 6 years ago

wendt88 commented 6 years ago

1) ERROR Error: java.lang.Exception: Failed resolving method setEnabled on class android.widget.Spinner -> items are a ValueList

<DropDown #dd
            [hint]="'application' | L"
            [items]="items"
            [(ngModel)]="itemIndex"
            (selectedIndexChanged)="itemSelect($event)"
            [isEnabled]="items.length"></DropDown>

2) ERROR Error: No value accessor for form control with unspecified name attribute -> only if my ValueList is empty (I fetch the items for the ValueList via api call, so I get this error and no [text] or [hint] attributes in the whole page get evaluated until I fill my ValueList). Removing [(ngModel)] solves the problem!

I tried to set as itemIndex various initial values (null, undefined, 0, etc)

PeterStaev commented 6 years ago

Hey @wendt88 , For 1 - this is a really strange error. setEnabled is a valid method for the spinner per the android docs.

For 2 - Make sure you are using the module as explained in the readme. Also you might have to initialize the items property and probably use the angular async pipe with it since you are retrieving the data with an API call.

wendt88 commented 6 years ago

For 2 - I initialize my items in ngOnInit as a empty ValueList and call on api request end multiple times the push method to write the items in it. I resolved it adding the ngDefaultControl attribute found here But now my itemIndex get not changed on select (selectedIndexChanged event gets called)

TextFields with [(ngModel)] works fine.

I use:

@angular: "~4.4.5"
nativescript-drop-down: "^3.2.0"
tns-core-modules: "~3.2.0"
PeterStaev commented 6 years ago

How are you registering the module? You should not use ngDefaultControl and the widget already implements its own value accessor.

wendt88 commented 6 years ago

I started with the NativeScript/sample-Groceries repository

and imported the DropDownModule in the app.module.ts.

The drop is in my login component which is declared in the login.module.ts

PeterStaev commented 6 years ago

I'm not sure what could be wrong. I would suggest to look at the demo-ng project in this repo and see what you are doing differently in your project so you can isolate what the problem is.

wendt88 commented 6 years ago

Problem 2 solved: I have to import the DropDownModule in the submodule (in groceries sample is it the LoginModule and GrocieruesModule) instead of the AppModule.

Now I created my own "SharedModule", where I import the DropDownModule (here you can also declare your own pipes, services, components etc. if somene is interested) and export everything of it.

Thank you for your help!