我无法确定如何通过ngModel
安全地解码某些字符串。
我目前正在使用ngSanitize
在视图中正确输出它,但在使用ngModel
时,它会完全中断。
{
id : 1,
name : 'Gary's Company'
}
// Returned from the API
$scope.user = {
id : 1,
name : 'Gary's Company'
};
<!-- Since ngModel isn't hooked up to the sanitizer, the text input field is showing the raw ASCII code. -->
<input type="text" ng-model="user.name" />
<!-- This displays "Gary's Company" correctly. The single quote gets convereted successfully. -->
<span ng-bind-html="user.name"></span>
我是在正确的轨道上还是有更好或更直接的方法?
答案 0 :(得分:4)
您可以使用$sanitize服务解码控制器中的值。
$scope.user = {
id : 1,
name : 'Gary's Company'
};
$scope.user.name = $sanitize($scope.user.name);
$sanitize
不是核心Angular包的一部分,因此您需要包含angular-sanitize.js
并在ngSanitize
模块上添加依赖项:
var app = angular.module('myApp', ["ngSanitize"]);