Angular.js中下拉框?qū)崿F(xiàn)渲染html的方法
前言
大家都知道angualrjs處于安全的考慮,插值 指令會對相應(yīng)字符串進(jìn)行過濾,避免出現(xiàn)html攻擊。但是在一些時候,我們需要渲染html,比如實(shí)現(xiàn)一個分級的下拉框
代碼如下:
<body ng-app="app" ng-controller="controller">
<select ng-model="value" ng-options="t.text for t in testList"></select>
<script src="/bootstrap/bootstrap/dist/angular-bootstrap/angular.js"></script>
<script type="text/javascript">
var app= angular.module("app",[]);
app.controller("controller",["$scope",function ($scope) {
var testList=[{id:0,text:" 全國"},{id:1,text:" 北京"},{id:20,text:" 上海"},{id:3,text:" 福建"},{id:4,text:" 山東"}];
$scope.value=20;
$scope.testList=testList;
}]);
</script>
</body>
可以看到,空格直接被渲染為 。一個簡單粗暴的解決辦法是修改angularjs源代碼,不再對html進(jìn)行過濾,在angularjs源碼中搜索updateOptions函數(shù),直接對相應(yīng)腳本進(jìn)行替換,如下圖:
可以看到,空格已經(jīng)被正確的渲染,這種方式雖然簡單,但是修改將會影響到所有的下拉框控件,有可能會受到html攻擊,一種比較中規(guī)中矩的辦法是采用ng-bind-html來渲染html,這個時候下拉框綁定數(shù)據(jù)的方式也需要改變,相應(yīng)代碼如下:
<body ng-app="app" ng-controller="controller">
<select ng-module="value" >
<option ng-repeat="data in testList" value="{{data.id}}" ng-selected="data.id==value" ng-bind-html="data.text">
</option>
</select>
<script src="/bootstrap/bootstrap/dist/angular-bootstrap/angular.js"></script>
<script type="text/javascript">
var app= angular.module("app",[]);
app.controller("controller",["$scope","$sce",function ($scope,$sce) {
var testList=[{id:0,text:" 全國"},{id:1,text:" 北京"},{id:20,text:" 上海"},{id:3,text:" 福建"},{id:4,text:" 山東"}];
for(var i=0;i<testList.length;i++)
{
testList[i].text=$sce.trustAsHtml( testList[i].text);
}
$scope.value='20';//注意,此處必須為字符串類型,否則無法獲取選中的值
$scope.testList=testList;
}]);
</script>
</body>
這種方式非常消耗性能,對于數(shù)據(jù)量不大的下拉框,這種方式完全可以滿足需要,但是如果數(shù)據(jù)量稍微大些,瀏覽器就會出現(xiàn)明顯的卡頓現(xiàn)象,這個時候可以自己寫一個指令來實(shí)現(xiàn)下拉框,代碼如下:
<body ng-app="app" ng-controller="controller">
<drop-down-list d-list="testList" value="id" text="text" d-select-value="value" ></drop-down-list>
{{value}}
<script src="/bootstrap/bootstrap/dist/angular-bootstrap/angular.js"></script>
<script type="text/javascript">
var app= angular.module("app",[]);
app.controller("controller",["$scope","$window",function ($scope,$window) {
var testList=[{id:0,text:" 全國"},{id:1,text:" 北京"},{id:20,text:" 上海"},{id:3,text:" 福建"},{id:4,text:" 山東"}];
$scope.value=20;
$scope.testList=testList;
}]);
app.directive("dropDownList",function () {
return{
restrict:'E',
scope :{
dList:'=',
dSelectValue:'='
} ,
link:function(scope, element, attrs) {
var d=document;
var value=attrs["value"];//對應(yīng)option的value
var text=attrs["text"];
var selectValue=scope.dSelectValue;
element.on("change",function(){
var selectedIndex=this.selectedIndex;
scope.$apply(function(){
scope.dSelectValue=selectedIndex;
});
})
for(var i=0;i<scope.dList.length;i++)
{
var option=d.createElement("option");
option.value=scope.dList[i][value];
option.innerHTML=scope.dList[i][text];
if(selectValue==option.value)
{
option.setAttribute("selected",true);
}
element.append(option);
}
},
template:'<select></select>',
replace:true
};
});
</script>
</body>
這種方式可以比較完美的實(shí)現(xiàn)相應(yīng)功能,是一種較好的選擇。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
AngularJS 表單驗(yàn)證手機(jī)號的實(shí)例(非必填)
下面小編就為大家?guī)硪黄狝ngularJS 表單驗(yàn)證手機(jī)號的實(shí)例(非必填)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
angularJs中$scope數(shù)據(jù)序列化的實(shí)例
今天小編就為大家分享一篇angularJs中$scope數(shù)據(jù)序列化的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
AngularJS解決ng界面長表達(dá)式(ui-set)的方法分析
這篇文章主要介紹了AngularJS解決ng界面長表達(dá)式(ui-set)的方法,通過具體問題的分析并結(jié)合實(shí)例形式給出了AngularJS長表達(dá)式的相關(guān)使用技巧,需要的朋友可以參考下2016-11-11
利用Angular2 + Ionic3開發(fā)IOS應(yīng)用實(shí)例教程
這篇文章主要給大家介紹了關(guān)于利用Angular2 + Ionic3開發(fā)IOS應(yīng)用的相關(guān)資料,文中通過示例代碼和圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
Angular實(shí)現(xiàn)svg和png圖片下載實(shí)現(xiàn)
這篇文章主要介紹了Angular實(shí)現(xiàn)svg和png圖片下載實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Angular2使用Guard和Resolve進(jìn)行驗(yàn)證和權(quán)限控制
本篇文章主要介紹了Angular2使用Guard和Resolve進(jìn)行驗(yàn)證和權(quán)限控制,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04

