AngularJS怎么更改html的属性值

AngularJS怎么更改html的属性值,第1张

angular框架继承了jQuery,可以使用JQuery方式修改,如$('div').attribute('data-key', 'new-value')

另一种办法就是使用动态数据咯,如<div data-key="{parameterName}">,当parameterName的值改变时data-key属性自动就更新了,例$scope.parameterName='new value'

用{{}}是一定没法输出html的

不知道你项目里是怎么限制的不能使用 ng-bind-html

那你试试自己写个指令来实现一下好了

var app = angular.model('app' , [])

app.directive('stringHtml' , function(){

  return function(scope , el , attr){

    if(attr.stringHtml){

      scope.$watch(attr.stringHtml , function(html){

        el.html(html || '')//更新html内容

      })

    }

  }

})

app.controller('TestCtrl' , function($scope){

  $scope.string = '<button type="button">测试html按钮</button>'

})

html:

<div ng-controller="TestCtrl">

  <span string-html="string"></span>

</div>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/8320067.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-15
下一篇2023-04-15

发表评论

登录后才能评论

评论列表(0条)

    保存