steve k steve k - 2 months ago 16
AngularJS Question

setting ng-bind-html on an iterating table in cshtml

I am setting ng-init

<td ng-init="myInit='myInit='@deal.OriginText')'">

based on the foreach statement below

@foreach (var deal in @Model.Deals.Take(Model.NumberOfFaresToDisplay ?? 6))

Inside of my html everything looks fine.

@deal.OriginText // returns the city && correct for each td

Inside of my javascript I set myInit to a scope key value pair

var currentFlight = scope.myInit; //exists inside of a ng-click function

CurrentFlight is set on

scope.openPriceChart = function (index) {
currentFlight //always the value of last ng-init on td from foreach

Not sure how to get the correct one. Any insight would be greatly appreciated.


While nothing is wrong with your code, the current setup is overwriting the myInit value, so you always see the last one set. Try this option

Using data attributes here to get the value set

<td data-myvalue = '@deal.OriginText' ng-click="DoSomething($event)">@deal.OriginText</td>

Script update

 $scope.DoSomething= function(abc){

This is from my understanding that you are a attempting to get the value of @deal.OriginText set on td element. An alternative/better approach would be to directly pass DoSomething('@deal.OriginText') instead of $event .Let us know if this is not what you wanted..