我们可以在jQuery HTML函数中使用angularJS字符串插值({{}})吗?

时间:2019-05-29 09:16:38

标签: jquery html angularjs angularjs-interpolate

我想在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

1 个答案:

答案 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));
  }
});

希望有帮助!