Ionic 2 - Any type variables

1

I'm having trouble migrating from ionic beta.11 to RC0. Every time I put a constructor expecting the type "any" I get the following error.

  

ngc error: Error: Error at   /Project/.tmp/app/app.module.ngfactory.ts:456:134: Property 'any' does   not exist on type 'typeof "/Project/.tmp/directives/mask"'.

By going to the app.module.ngfactory.ts at line 456 it generates the line below:

if ((this.__MaskDirective_75 == (null as any))) { (this.__MaskDirective_75 = new import45.MaskDirective(this.parent.get(import45.any),this.parent.get('mask'))); }

Here is the code for my policy:

import {Directive, Attribute} from '@angular/core';

@Directive({
    selector: '[mask]',
    host: {
        '(keyup)': 'onInputChange()'
    }
})
export class MaskDirective {
    maskPattern: string;
    placeHolderCounts: number;
    dividers: string[];
    modelValue: string;
    viewValue: string;

    constructor(
        public model: any,
        @Attribute("mask") maskPattern: string
    ) {
        this.dividers = maskPattern.replace(/\*/g, "").split("");
        this.dividers.push(" ");
        this.generatePattern(maskPattern);
    }

    onInputChange() {
        this.modelValue = this.getModelValue();
        var stringToFormat = this.modelValue;
        if (stringToFormat.length < 10) {
            stringToFormat = this.padString(stringToFormat);
        }

        this.viewValue = this.format(stringToFormat);
        this.model.viewToModelUpdate(this.modelValue);
        this.model.valueAccessor.writeValue(this.viewValue)
    }

    generatePattern(patternString) {
        this.placeHolderCounts = (patternString.match(/\*/g) || []).length;
        for (let i = 0; i < this.placeHolderCounts; i++) {
            patternString = patternString.replace('*', "{" + i + "}");
        }
        this.maskPattern = patternString;
    }

    format(s) {
        var formattedString = this.maskPattern;
        for (let i = 0; i < this.placeHolderCounts; i++) {
            formattedString = formattedString.replace("{" + i + "}", s.charAt(i));
        }
        return formattedString;
    }

    padString(s) {
        var pad = "          ";
        return (s + pad).substring(0, pad.length);
    }

    getModelValue() {
        var modelValue = this.model.value;
        for (var i = 0; i < this.dividers.length; i++) {
            while (modelValue.indexOf(this.dividers[i]) > -1) {
                modelValue = modelValue.replace(this.dividers[i], "");
            }
        }
        return modelValue;
    }
}

What can I do to correct this problem and continue updating my project?

    
asked by anonymous 04.10.2016 / 20:15

1 answer

0

This happens because the DI (dependency injection) system knows what it needs to inject according to the type of the parameter in the constructor ().

Example:

constructor(
    private navController: NavController, 
    private http: Http) { }

So Angular2 knows it has to "give a new" in the NavController and Http classes.

In your case, as it is like any, it does not know what to inject. There are a few ways to solve your problem.

1: I believe that in your case, you need to inject NgModel

import {NgModel} from '@angular/forms';
...
constructor(
    public model: NgModel,
    @Attribute("mask") maskPattern: string) {
...
}

2: Tell the guy who manages the dependency injections (container DI), which he should send whenever he needs to give a new one in the MaskDirective class.

In this case I will not be able to help you, I did not find any practical examples.

Useful links:

1 - Related Issue

2 - Documentation @Directive

3 - Dependency Injection Examples

    
29.01.2017 / 17:29