【问题标题】:Can we use angularJS string interpolation ( {{}}) in jquery html function我们可以在 jquery html 函数中使用 angularJS 字符串插值({{}})吗
【发布时间】:2019-05-29 09:16:38
【问题描述】:

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

【问题讨论】:

    标签: jquery html angularjs angularjs-interpolate


    【解决方案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));
      }
    });
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-31
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      相关资源
      最近更新 更多