javascript - 我们如何在通过 Controller 附加到 html 页面的 html 中使用 angularjs 作用域变量?

标签 javascript html angularjs

我有以下代码

var input = '<div class="form-group">{{variable}}</div>'

我有上面的代码,它是写在 Controller 内的。我想将此元素插入 View 中,并且希望从 Controller 绑定(bind)此变量值,但它只是打印与 {{variable}} 相同的内容。谁能告诉我该怎么做?

最佳答案

ng-bind-html指令将绑定(bind) html,范围变量将分配给 Controller 本身

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $sce) {
    $scope.variable = 'test'
    $scope.firstName = $sce.trustAsHtml("<h1>"+$scope.variable+"</h1>");
    
});
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">

    <div ng-bind-html="firstName"></div>

</body>

关于javascript - 我们如何在通过 Controller 附加到 html 页面的 html 中使用 angularjs 作用域变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60412034/

相关文章:

javascript - Angularjs javascript 加载

javascript - AngularJS 将我的 ng-href url "slash"转换为 "%2f"

javascript - 使用键盘控制更改日期

javascript推送返回数字而不是对象

html - 滚动带有内容的固定标题

javascript - 使用 `encodeURIComponent` 编码 URL 不适用于表单操作

javascript - Angular js foreach只返回数组中的最后一项

javascript - 获取父元素

javascript - Kalendae js : Is there a way to automatically close the calendar once a date has be selected?

java - 如何仅从 HTML 页面中提取主要文本内容?