0

我想在 Jquery html 函数中使用 angularJs 字符串插值

$('#existingScoresForWeek').html("<p>{{1 + 1}}</p>");

上面的代码行没有打印 Result as 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

4

1 回答 1

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

希望能帮助到你!

于 2019-05-29T09:30:16.093 回答