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 !"
}
}
哪个后端您使用的?你的功能在打电话吗? –
@Mayur Patel我编辑了这个问题,并给出了我的代码... – saif
@Darshak Gajjar它不是在角度2 – saif