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