我的String包含一些HTML标记
例如:
$scope.string = '<div> <span> some text </span> </div>';
我想将其转换为html元素并进行一些更改
所以我做到了:
$scope.stringAfterEdit = angular.element($scope.string).find("span").addClass('newClass');
然后,我需要在编辑后将html附加到页面上。
所以我尝试了这个
<div ng-bind-html="stringAfterEdit"></div>
但是没有用。
知道怎么做吗?
预先感谢。
答案 0 :(得分:0)
您需要使用$sce
:
$scope.string = '<div> <span class="newclass"> some text </span> </div>';
$scope.trustedHtml = $sce.trustAsHtml($scope.string);
,在这里您不能从CSS添加任何类。像newClass
然后是html:
<div ng-bind-html="trustedHtml"></div>
答案 1 :(得分:0)
我通过这种方式解决了这个问题:
注意 我正在将Jquery与angularJS一起使用
@Configuration
@EnableRedisHttpSession
public class RedisHttpSessionConfig extends
AbstractHttpSessionApplicationInitializer {
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
public RedisTemplate<Object, Object> redisTemplate() {
final RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
和HTML
extends AbstractHttpSessionApplicationInitializer
答案 2 :(得分:0)
尝试一下:
var string = '<div><span> some text </span> </div>';
var ele = angular.element(string);
ele.find("span").addClass('newClass');
$scope.trustedHtml = $sce.trustAsHtml(ele.html());
html:
<div ng-bind-html="trustedHtml"></div>