Search code examples
javascriptangularjsjavascript-injection

How to access Angular attribute via Javascript injection


For instance, let's say we have

<div class="row costBreakdown__shipping">
            <div class="small-8 medium-7 large-7 columns">Shipping:</div>
            <div class="small-4 medium-5 large-5 columns text-right" ng-switch="cart.isRated">
                <!-- ngSwitchWhen: true --><div ng-switch-when="true" class="ng-binding ng-scope">$8.48</div><!-- end ngSwitchWhen: -->
                <!-- ngSwitchWhen: false -->
            </div>
        </div>

How would one access the value $8.48 in the class costBreakdown__shipping? I've have

var x = $(".costBreakdown__shipping").eq(2).children().eq(1).children().eq(1) 

which gives me

<!-- ngSwitchWhen: true --><div ng-switch-when="true" class="ng-binding ng-scope">$8.48</div>

but how would I go about accessing the value inside?


Solution

  • Have you tried using .innerHTML at the end of your assignment value to x?

    var x = $(".costBreakdown__shipping").eq(2).children().eq(1).children().eq(1).innerHTML 
    

    If you are using Angular, why not assign it to a variable and declare that in your "Shipping" controller (or whatever controller you're using)

    <div ng-switch-when="true" class="ng-binding ng-scope">{{shippingCost}}</div>
    

    And in your controller, you can declare

    $scope.shippingCost = '$8.48';
    

    Or try shipping cost = 8.48 and apply a filter for currency on the shippingCost in your html