无法在ng

编程入门 行业动态 更新时间:2024-10-28 21:14:24
无法在ng-click功能上获取ng-bind-html下拉值(not able to get ng-bind-html dropdown value on ng-click function)

我是Angularjs的新手,我可以在Jquery中做到这一点,但我想学习angularjs

我有下拉使用ng-bind-html获取下拉值并将该值更新为ng-bind-html它工作正常。 但是当我从下拉列表中选择值并在控制台日志中进行控制时,它显示为空。

但是当我用硬编码值更新下拉列表并选择值时,它会在日志中正确显示。

HTML代码:

<div ng-app="myApp"> <div ng-controller="addComplaintCtrl"> <select class="form-control" id="product_name" name="product_name" ng-model="product_name" ng-bind-html="pdl"> <!--<option value="">-Select-</option> <option value='00'>Other</option>--> </select> <input type="button" class="btn btn-primary btn-block" ng-click="complaintSubmitEvent();" value="Submit Complaint"> </div> </div>

角码:

<script> var app = angular.module('myApp',['ngSanitize']); app.controller("addComplaintCtrl",function($scope,$sce){ $scope.pdl=$sce.trustAsHtml('<option value="">-Select-</option><option value="Sales" >Sales</option>'); $scope.complaintSubmitEvent=function(){ //alert(0); console.log($scope.product_name); } }) </script>

下面是jsfiddle链接。 NG绑定,HTML

i new to Angularjs i can do this in Jquery but i want to learn angularjs.

i have dropdown were im using ng-bind-html to get dropdown value and updating that value to that ng-bind-html it is working fine. but when i select the value from dropdown and console it in console log it is showing as null.

but when i update the dropdown with hardcode value and select the value then it is coming correctly on log.

HTML Code:

<div ng-app="myApp"> <div ng-controller="addComplaintCtrl"> <select class="form-control" id="product_name" name="product_name" ng-model="product_name" ng-bind-html="pdl"> <!--<option value="">-Select-</option> <option value='00'>Other</option>--> </select> <input type="button" class="btn btn-primary btn-block" ng-click="complaintSubmitEvent();" value="Submit Complaint"> </div> </div>

Angular Code:

<script> var app = angular.module('myApp',['ngSanitize']); app.controller("addComplaintCtrl",function($scope,$sce){ $scope.pdl=$sce.trustAsHtml('<option value="">-Select-</option><option value="Sales" >Sales</option>'); $scope.complaintSubmitEvent=function(){ //alert(0); console.log($scope.product_name); } }) </script>

below is jsfiddle link. ng-bind-html

最满意答案

您必须编译ng-bind-html所做的更改,您可以创建类似的指令

app.directive('compile',function($compile, $timeout){ return{ restrict:'A', link: function(scope,elem,attrs){ $timeout(function(){ $compile(elem.contents())(scope); }); } }; });

在ng-bind-html完成其工作后, $timeout用于运行编译功能

现在你可以简单地使用ng-bind-html将compile作为select的属性

<select class="form-control" id="product_name" name="product_name" ng-model="product_name" ng-bind-html="pdl" compile >

工作小提琴

You have to compile the change made by ng-bind-html you can create a directive something like

app.directive('compile',function($compile, $timeout){ return{ restrict:'A', link: function(scope,elem,attrs){ $timeout(function(){ $compile(elem.contents())(scope); }); } }; });

$timeout is used to run compile function, after ng-bind-html do its job

Now you can just simply put compile as attribute of select with ng-bind-html

<select class="form-control" id="product_name" name="product_name" ng-model="product_name" ng-bind-html="pdl" compile >

Working fiddle

更多推荐

本文发布于:2023-07-08 05:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1072487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ng

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!