2017-02-21 99 views
-1

JavaScript可以改变HTML内容的HTML内容是这样的:如何改变在角2

<p id="demo">JavaScript can change HTML content.</p> 
<button type="button" onclick="document.getElementById('demo').innerHTML = 'Hello JavaScript!'">Click Me!</button> 

jQuery的可以改变的HTML内容是这样的:

<script> 
$(document).ready(function(){ 
    $("button").click(function(){ 
    $("#demo").html("Hello JQuery!"); 
    }); 
}); 
</script> 
</head> 
<body> 
<p id="demo">JQuery can change HTML content.</p> 
<button type="button" >Click Me!</button> 

AngularJS可以改变HTML内容是这样的:

<div ng-app="myApp" ng-controller="myCtrl"> 
    <p id="demo">{{content}}</p> 
    <button type="button" ng-click=changeHtmlContent()>Click Me!</button> 
</div> 
<script> 
var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope) { 
    $scope.content="AngularJs can change HTML content." 
    $scope.changeHtmlContent = function() { 
    $scope.content = "Hello Angular Js!"; 
    }; 
}); 
</script> 

我想用角2组件做同样的事情。但它不起作用。

@Component({ 
    selector: 'my-app', 
    template: ` 
    <p id="demo">{{content}}</p> 
    <button type="button" ng-click=changeHtmlContent()>Click Me!</button> 
    `, 
}) 
export class App { 
    content:string="Angular2 can change HTML content"; 
    constructor() {} 
    //this data may come from api 
    changeHtmlContent(){ 
    this.content ="Hello Angular2 !" 
    } 
} 
+0

哪个后端您使用的?你的功能在打电话吗? –

+0

@Mayur Patel我编辑了这个问题,并给出了我的代码... – saif

+0

@Darshak Gajjar它不是在角度2 – saif

回答

2

您必须使用(click)

<button type="button" (click)="changeHtmlContent()">Click Me!</button> 
+1

其作品。 ng-click将无法为angular2 – saif

+0

@ Md.SaifulIslam检查它作为答案然后:) – developer033

+0

谁downvoted,可以解释为什么? – developer033

-1

我认为here是您的解决方案。

<p id="demo" [innerHTML]="content"></p>