2016-03-06 54 views
0

我正在使用AngularJS構建帶有Ionic的移動應用程序。鏈接無法在移動設備上使用ng-bind-html

在一些視圖中,我想綁定具有多個鏈接的HTML代碼,但某種程度上它不適用於移動設備。

在瀏覽器中,它的工作原理非常完美,但是在移動設備上,鏈接無法點擊。

文字,我想結合:

"Some text <a href="http://www.test.com" target="_blank">http://www.test.com</a>" 

我的HTML代碼:

<p ng-bind-html="testDetails"></p> 

$消毒可用,ngSanitize已添加作爲一個依賴

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular-sanitize.js"></script> 
var appControllers = angular.module('starter.controllers', ['ngSanitize']); // Use for all controller of application. 

任何理念?

回答

0

看來我找到了解決方案。

不知何故簡單<a>帶有href屬性的標籤似乎不適用於使用ng-bind-html的移動設備。

相反,我用:

<a href="" onClick="window.open('http://www.test.com', '_system', 'location=yes')" 
target="_blank">http://www.test.com</a> 

這僅僅完美,但(見AngularJS文檔),有必要繞過$通過明確信任的危險值以ng綁定HTML的消毒。 https://docs.angularjs.org/api/ngSanitize/service/ $消毒

在控制器:

$scope.testDetails = '<a href="" onClick="window.open('http://www.test.com', '_system', 'location=yes')" 
target="_blank">http://www.test.com</a>' 

$scope.deliberatelyTrustDangerousSnippet = function(sniptext) { 
    return $sce.trustAsHtml(sniptext); 
}; 

在HTML視圖:

<p ng-bind-html='deliberatelyTrustDangerousSnippet(testDetails)'></p> 

而且我找到了一個很好的過濾器來完成這項工作,如果接收到的數據用簡單的<a href="">屬性: https://gist.github.com/rewonc/e53ad3a9d6ca704d402e

相關問題