Encode value of two way binding

javascript angular

56 просмотра

1 ответ

3478 Репутация автора

I have a two way binding with a property on my component. The purpose is to take a URL from the users and then redirect through to /report/<url>.

Problem is that it beaks on the url and re-directs to report/http%3A and misses off the domain.

This is what I have so far:


<input type='text' [(ngModel)]='testUrl' />
<button class='btn btn-primary'(click)='checkMobile()'>Check Site</button>


import { Component }  from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';

    selector: "check-mobile",
    templateUrl: "app/check-mobile.component.html"
export class CheckMobileComponent{
    testUrl: string;

    constructor(private router: Router)


    checkMobile() : void {

        var escapedUrl = encodeURI(this.testUrl);
        this.router.navigateByUrl('/report/' + escapedUrl);

What am I doing wrong?

Автор: Guerrilla Источник Размещён: 10.03.2017 02:50

Ответы (1)

2 плюса

9669 Репутация автора


use QueryEncoder provided by Angular 2. Do this

import { QueryEncoder } from '@angular/http';

var encodedUrl = new QueryEncoder().encodeValue(this.testUrl);
this.router.navigate(['/report/' + escapedUrl]);
Автор: Eduardo Dennis Размещён: 10.03.2017 03:02
Вопросы из категории :