使用来自firebase数据库的现有数据填充表单输入值

时间:2016-06-20 20:26:29

标签: angular firebase angularfire

我有一个表单,用户可以通过该表单更新现有数据。

public final class CalcUtils {
private CalcUtils(){

}

public static boolean isOperator(char c){
    return c == '/' ||
            c == '÷' ||
            c == '*' ||
            c == '-' ||
            c == '+';
}

public static double evaluate(final String str){
    class Parser {
        int pos = -1, c;

        void eatChar() {
            c = (++pos < str.length()) ? str.charAt(pos) : -1;
        }

        void eatSpace() {
            while (Character.isWhitespace(c)) eatChar();
        }

        double parse() {
            eatChar();
            double v = parseExpression();
            if (c != -1) throw new RuntimeException("Unexpected: " + (char)c);
            return v;
        }

        // Grammar:
        // expression = term | expression `+` term | expression `-` term
        // term = factor | term `*` factor | term `/` factor | term brackets
        // factor = brackets | number | factor `^` factor
        // brackets = `(` expression `)`

        double parseExpression() {
            double v = parseTerm();
            for (;;) {
                eatSpace();
                if (c == '+') { // addition
                    eatChar();
                    v += parseTerm();
                } else if (c == '-') { // subtraction
                    eatChar();
                    v -= parseTerm();
                } else {
                    return v;
                }
            }
        }

        double parseTerm() {
            double v = parseFactor();
            for (;;) {
                eatSpace();
                if (c == '/' || c == '÷') { // division
                    eatChar();
                    v /= parseFactor();
                } else if (c == '*' || c == '(') { // multiplication
                    if (c == '*') eatChar();
                    v *= parseFactor();
                } else {
                    return v;
                }
            }
        }

        double parseFactor() {
            double v;
            boolean negate = false;
            eatSpace();
            if (c == '+' || c == '-') { // unary plus & minus
                negate = c == '-';
                eatChar();
                eatSpace();
            }
            if (c == '(') { // brackets
                eatChar();
                v = parseExpression();
                if (c == ')') eatChar();
            } else { // numbers
                int startIndex = this.pos;
                while ((c >= '0' && c <= '9') || c == '.') eatChar();
                if (pos == startIndex) throw new RuntimeException("Unexpected: " + (char)c);
                v = Double.parseDouble(str.substring(startIndex, pos));
            }

            eatSpace();
            if (c == '^') { // exponentiation
                eatChar();
                v = Math.pow(v, parseFactor());
            }
            if (negate) v = -v; // unary minus is applied after exponentiation; e.g. -3^2=-9
            return v;
        }
    }
    return new Parser().parse();
}

此表单的组件类

  <input type="text" class="form-control" id="name" [(ngModel)]="memberPristine.name" 
    #name="ngForm" ngControl="name">

用户服务类

form: ControlGroup;
memberPristine: {
    name: ''
};
constructor(private fb: FormBuilder, private userService: UserService) {
}

private buildForm(): void{
 this.form = this.fb.group({
     name: ['', Validators.required]
 })
} 
 ngOnInit() {
   this.userService.member$.subscribe(x=>{
      this.memberPristine = x;
   });
    this.buildForm();
  }

尽管member $是一种FirebaseObjectObservable,但是无法从组件类访问它。 当我运行这个时,我收到了一个错误。 export class UserService { member$: FirebaseObjectObservable<any>; constructor(public af: AngularFire) { this.af.auth.subscribe(a => { this.member$ = this.af.database.object('/members/'+a.uid); }); }

0 个答案:

没有答案