2017-03-12 19 views
1

我試圖使用Angular Js Post HTTP方法獲取HTML內容。這隻能處理像P標籤這樣的文本內容。但我想要使用Angular JS(如文本框)等獲取Input類型的HTML內容。當我使用Ajax Load語法時,我也可以獲得所有HTML包含輸入類型的HTML內容。如何獲取HTML包含使用HTTP發佈由Angular JS像Ajax負載

代碼JS:

$http.post("/user/users_data/about/bidata.php", 
{ 
    "type": "get", 
    "user": "<?php echo $_GET['u']; ?>" 
}).then(function(response) 
{ 
    $scope.bidata = response.data; 
}, function(error) 
{ 
}); 

HTML代碼:

<span ng-bind-html="bidata"></span> 

我已經添加上述下面的東西。

var app = angular.module('userapp', ['ngSanitize']); 


app.controller('usercontroller', function($scope, $http) 

bidata.php文件的HTML代碼:

<div class="form-group row"> 
    <p class="col-2 col-form-label">Text</p> 
    <div class="col-10"> 
    <input class="form-control" type="text" value="Artisanal kale" > 
    </div> 
</div> 
<div class="form-group row"> 
    <p class="col-2 col-form-label">Search</p> 
    <div class="col-10"> 
    <input class="form-control" type="search" value="How do I shoot web" > 
    </div> 
</div> 

只有我可以得到文字和搜索p標籤HTML內容。

當我使用$ scope.bidata = $ sce.trustAsHtml(response.data);,我在控制檯日誌中出現錯誤。

ReferenceError: $sce is not defined 
    at ?u=aa:73 
    at angular.min.js:131 
    at m.$eval (angular.min.js:145) 
    at m.$digest (angular.min.js:142) 
    at m.$apply (angular.min.js:146) 
    at l (angular.min.js:97) 
    at J (angular.min.js:102) 
    at XMLHttpRequest.t.onload (angular.min.js:103) 
+0

如果控制檯登錄響應數據是什麼? – CaptainHere

+0

檢查我的答案@ user38701 –

+0

的ReferenceError:$ SCE沒有在規定的 U = AA:73 在angular.min.js:131 在M $的eval(angular.min.js:145) 在m。在J(angular.min.js:102)處在l(angular.min.js:97) 處應用(angular.min.js:146) 處的$ digest(angular.min.js:142) 。 在XMLHttpRequest.t.onload(angular.min.js:103) – user38701

回答

2

你必須做2件事。

使用data-ng-bind-html=""和 使用$sce.trustAsHtml(string)

$http.post("/user/users_data/about/bidata.php", 
{ 
    "type": "get", 
    "user": "<?php echo $_GET['u']; ?>" 
}).then(function(response) 
{ 
    $scope.bidata =$sce.trustAsHtml(response.data); 
}, function(error) 
{ 
}); 

控制器定義

app.controller('usercontroller', function($scope, $http,$sec) 

瞭解sec

+0

如果我把$ sce.trustAsHtml(response.data),我什麼也得不到。 – user38701

+1

控制檯日誌說ReferenceError:$ sce沒有定義 – user38701

+0

是的,你必須在你的控制器中使用$ http,$ sce定義它 –