Javascript 动态创建的ui sref链接不工作

Javascript 动态创建的ui sref链接不工作,javascript,angularjs,ionic-framework,angular-ui-router,Javascript,Angularjs,Ionic Framework,Angular Ui Router,拥有一个字典,我可以用它解析一些关键字上的文本,并向字典页面添加链接。看起来ui sref链接根本不起作用。以下是我的功能: /** * adds links to text instead of an dictionary word * @param {string} text */ Dictionary.prototype.wrapTextWordsInLinksToDictionary = function(text) { var i, len, pattern, re;

拥有一个字典,我可以用它解析一些关键字上的文本,并向字典页面添加链接。看起来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;
}
和模板的一部分:

//...
<p ng-bind-html="vm.disease.controlDesc"></p>
//...

正在分析控制器中的某些对象: 在控制器中注入$compile服务

if (typeof vm.disease[propName] === 'string') {
    vm.disease[propName] = Dictionary.wrapTextWordsInLinksToDictionary(vm.disease[propName]);
    vm.disease[propName] =  $compile(vm.disease[propName])($scope);
}

你能显示你的$stateProvider吗?@rkalita更新了你需要使用的post
$compile
,如果你想在HTML中使用angular/javascript,它位于你想通过
ng bind HTML
注入的其他变量中。不过,我会让其他人来写一个完整的答案来解释它是如何工作的,因为我根本不提倡创建使用
ngbindhtml
的角度逻辑。这种逻辑最好由修改DOM的指令来处理,而不是由注入变量的DOM来处理。以这种方式编程是一种代码味道,表明您正试图强迫angular像其他框架一样工作。@Claies是的,您说得很对-我在这里急于解决问题太多了。。。在那里我可以使用指令
.state('app.dictionary', {
  url: '/dictionary',
  views: {
    'menuContent': {
      templateUrl: 'templates/dictionary.html',
      controller: 'DictionaryStateController',
      controllerAs: 'vm',
    }
  }
})
if (typeof vm.disease[propName] === 'string') {
    vm.disease[propName] = Dictionary.wrapTextWordsInLinksToDictionary(vm.disease[propName]);
    vm.disease[propName] =  $compile(vm.disease[propName])($scope);
}