0
有一本词典,我用它解析关键字上的一些文本,并将链接添加到词典页面。看起来ui-sref链接根本不起作用。这里是我的功能:动态创建的ui-sref链接不起作用
/**
* adds links to text instead of an dictionary word
* @param {string} text
*/
Dictionary.prototype.wrapTextWordsInLinksToDictionary = function(text) {
var i, len, pattern, re;
// internal wrapping fn
function wrapInLink(match) {
var result = '<a ui-sref="app.dictionary">' + match + '</a>';
// console.log(result);
return result;
}
for (var modelName in this.words) {
i = this.words[modelName].word.length - 1;
// starting from plurals and not doing singular if plural is done already
while (i >= 0) {
pattern = this.words[modelName].word[i];
re = new RegExp(pattern, "g");
text = text.replace(re, wrapInLink);
break;
i--;
}
}
return text;
}
在控制器解析一些对象:
//...
if (typeof vm.disease[propName] === 'string') {
vm.disease[propName] = Dictionary.wrapTextWordsInLinksToDictionary(vm.disease[propName]);
}
//...
和模板的一部分:
//...
<p ng-bind-html="vm.disease.controlDesc"></p>
//...
在控制台中没有错误,但链接根本无法点击。有人可以提供线索吗?干杯!
更新时,我把href="#/app/dictionary"
而不是ui-sref=...
它工作正常。
UPD 2 Stateprovider:
.state('app.dictionary', {
url: '/dictionary',
views: {
'menuContent': {
templateUrl: 'templates/dictionary.html',
controller: 'DictionaryStateController',
controllerAs: 'vm',
}
}
})
你能证明你的$ stateProvider? – rkalita
@rkalita更新了帖子 – nik
如果您打算在您想要通过ng-bind-html注入的其他变量内部使用HTML内的angular/javascript,则需要使用'$ compile'。我将把它留给其他人来写一个完整的答案,解释它是如何工作的,但是,因为我不主张创建使用'ng-bind-html'的角色逻辑。这种逻辑更好地通过修改DOM的指令处理,而不是注入到变量中的DOM。用这种方式编程是一种代码异味,并且表明您正试图强制角色像其他框架一样行事。 – Claies