使用指令编译HTML时,$ compile抛出错误

时间:2016-11-18 14:45:50

标签: javascript angularjs

我有一种情况,其中多个func applicationWasTerminated(notification: NSNotification?) { guard let notif = notification else { return } guard let userInfo = notif.userInfo as? [String : AnyObject] else { return } guard let identifier = userInfo["NSApplicationBundleIdentifier"] as? String else { return } if identifier == "com.apple.finder" { NSWorkspace.sharedWorkspace().launchAppWithBundleIdentifier("com.apple.finder", options: NSWorkspaceLaunchOptions.Default, additionalEventParamDescriptor: nil, launchIdentifier: nil) } } 元素由第三方Angular指令呈现。我想为每个<a href="..">Link!</a>元素添加另一个指令,我通常会使用<a>

如果可以的话,我会把它写成ng-bind-html

我想要添加的唯一部分是<a ng-href="item.Url" ng-bind-html="item.Name | highlight: $scope.searchTxt"></a>指令。我的一般想法是重新编译所有元素,并用新元素替换它们,如下所示

highlight

假设上面的字符串结果为angular.element('a') .each(function(index, val) { addHighlightDirective(val); }); var addHighlightDirective = function (el) { var compiledHtml = $compile("<a ng-href='" + el.href + "' ng-bind-html='" + el.innerHTML.toString() + ' | highlight: $scope.searchText' + "'></a>")($scope); //... //append to html //... } "<a ng-href='#welcomePage' ng-bind-html='Welcome to our site! | highlight: $scope.searchText'></a>"会抛出错误

$compile

Plnkr with error

我假设错误与Angular有某种关系,将字符串视为javascript,但我无法绕过它。

script.js:XXX Error: [$parse:syntax] Syntax Error: Token 'to' is an unexpected token at column 9 of the expression [Welcome to our site! | highlight: $scope.searchText] starting at [to our site! | highlight: $scope.searchText].如何对此进行评估,以及如何更改代码以使其正常工作?

0 个答案:

没有答案
相关问题