Angular2以编程方式提交POST

时间:2017-03-09 17:48:21

标签: angular

以下是以编程方式提交表单的jQuery

return this.http.post(GlobalVariables.SITE_ROOT + url, dto, { headers })
        .subscribe(function (data) {
        console.log('received response');
    });

我如何对Angular2做同样的事情?

以下代码的问题在于它是Ajax所以它需要返回响应。我不想回来,因为我想要一个新观点。

const data = [
  {
    "name": "Jumbo Lump Crab Cake",
    "description": "Creole lobster sauce."
  },

  {
    "name": "Bacon Wrapped Sea Scallops",
    "description": "Chardonnay lemon sauce, Mango salsa."
  },
  {
    "name": "Chilled Maine Lobster Cocktail",
    "description": "Atomic cocktail sauce and lemon butter."
  },
  {
    "name": "Coconut Shrimp",
    "description": "three jumbo tiger shrimp, vanilla orange beurre blanc."
  },
  {
    "name": "Crispy Point Judith Calamari",
    "description": "Italian peppers and Bleu cheese Stuffed olives, Atomic cocktail sauce."
  },
  {
    "name": "Oysters on the Half Shell",
    "description": "shucked to order, mignonette and Atomic cocktail sauces."
  }
]
const myHtml = [];
// loop through the data building up the html
data.forEach(value => myHtml.push(`<li>${value.name}</li><li>${value.description}</li>`));
// now render the html
$('#myData').append(myHtml.join(''))

非常感谢

1 个答案:

答案 0 :(得分:0)

您需要在帖子完成后使用路由器重定向到新的。

export class ExampleComponent
  constructor(private router: Router) {}

  public function submitAndRedirect() {
    return this.http.post(GlobalVariables.SITE_ROOT + url, dto, { headers })
        .subscribe(function (data) {
          this.router.navigate(['newRoute', {id: id}]);
    });
  }
}