如果打开新选项卡,ng-bind-html 中的过滤器会出错

Filter in ng-bind-html give an error if open new tab

我正在使用 HTML 换行过滤器将 \n 替换为 <br>,就像这样

angular
.module('App')
.filter('addHtmlLineBreaks', addHtmlLineBreaks);

function addHtmlLineBreaks () {
    return function (text) {
        var output = text.replace(/\n/g, '<br/>');
        return output;
    };
}

然后我使用 ng-bind-html

应用它
<p ng-bind-html="coach.Description | addHtmlLineBreaks"></p>

一切似乎都很好,但是我有一个 link 可以打开新标签页的地方

<a ui-sref="Detail" target="_blank">

现在过滤器仍然有效,但出现错误

angular.js:13920 TypeError: Cannot read property 'replace' of undefined
at mtc.min.js:1
at fn (eval at compile (angular.js:14817), <anonymous>:4:311)
at regularInterceptedExpression (angular.js:16043)
at expressionInputWatch (angular.js:15948)
at Scope.$digest (angular.js:17515)
at Scope.$apply (angular.js:17790)
at done (angular.js:11831)
at completeRequest (angular.js:12033)
at XMLHttpRequest.requestLoaded (angular.js:11966)

知道发生了什么以及如何解决吗?

确保文本已定义:

 angular.isDefined(text) ? text.replace(/\n/g, '<br/>') : ''