2016-04-11 48 views
0

所獲取的JSON數據我已經獲取的JSON數據,這是這個樣子:更改使用Angularjs

{ 
    "id": "154", 
    "user_id": "1445674241", 
    "title": "Power of one", 
    "content": "<p><img style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"/photo.png\" alt=\"Power of one\" width=\"455\" height=\"567\" /></p>\n<p>&nbsp;</p>\n<p>One is really powerful, you are one, God is one, earth is one. Love the one.<img style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"/photo.png\" alt=\"Power of one\" width=\"455\" height=\"567\" /></p>", 
    "num_views": "0", 
    "votes_up": "1", 
} 

我想改變這一切的img標籤的屬性(如追加SRC,改變高度&寬度)使用角度js。

我也在我的控制器中使用ng-bind-html呈現html代碼。其功能是:

$scope.renderHtml = function (html_code) { 
    return $sce.trustAsHtml(html_code); 
}; 

幫助我改變img標籤的屬性。

+1

這裏有什麼問題? – rupampatel

+0

想要更改img標籤的屬性。 –

回答

0

我認爲你必須讓你的手髒直接data.content字符串的工作...
事情是這樣的:

var newWidth = 123; 
data.content = data.content.replace(/(img.*?width=")(.*?)(")/g, function(c1, c2, c3) { return c1 + newWidth + c3; }); 
+0

什麼是c1,c2和c3? –

+0

我們在例子中使用的三個捕獲組的每一個('(...)')的內容...也許你會發現有用的閱讀http://www.w3schools.com/jsref/jsref_obj_regexp.asp。 .. – MarcoS

0

您可以創建爲某種自定義過濾器,爲前如果我m如果從我的數據輸入YouTube鏈接,我想在我的頁面級嵌入前對其進行修改

myApp.filter('trustedURL', ['$sce', function($sce){ 
    return function(input) { 
     return $sce.trustAsResourceUrl(input); 
    }; 
}]); 

myApp.filter('youtubeEmb', ['$sce', function($sce){ 
    return function(input) { 
     var shortYou = input.match(/youtu.be/g); 
     if(shortYou){ 
      return input.substring(0,input.indexOf('?')).replace(shortYou,"youtube.com/embed"); 
     }else{ 
      return input.substring(0,input.indexOf('&')).replace("watch?v=","embed/"); 
     } 
    }; 
}]); 

我的HTML的

<iframe src="data.uri | trustedURL | youtubeEmb"></iframe>

這裏是url,同樣可以爲img標籤attrs創建一些函數。