我想在Jquery html函数中使用angularJs字符串内插
$('#existingScoresForWeek').html("<p>{{1 + 1}}</p>");
上面的代码行未将结果打印为2
<div data-ng-app="myApp" ng-controller="myCtrl">
<p id="existingScoresForWeek"></p>
<button ng-click="myFunc()">sub</button>
</div>
<script>
var app = angular.module("myApp", ['ngSanitize']);
app.controller("myCtrl", function($scope) {
$scope.myFunc= function(){
ScoreScreenersSave();
};
function ScoreScreenersSave() {
$('#existingScoresForWeek').html("<p>{{1 + 1}}</p>");
}
});
</script>
实际结果:{{1 + 1}} 预期结果:2
答案 0 :(得分:1)
使用$compile
来编译您的html字符串。
见下文:
var app = angular.module("myApp",[]);
app.controller("myCtrl", function($scope, $compile) {
$scope.myFunc= function(){
ScoreScreenersSave();
};
function ScoreScreenersSave() {
$('#existingScoresForWeek').html($compile("<p>{{1 + 1}}</p>")($scope));
}
});
希望有帮助!