1

I have issue with get value from custom data attribute to controller. I want to console.log value from data-key attribute.

I tried use $attrs but is unknown, please for hint or advice.

HTML:

<div class="offer-box" ng-repeat="offer in offers.offerDtoList">
              <div class="offer-box__headline">
                  <div class="offer-box__lp"><span class="number">{{ offer.spaceId }}.</span></div>
                  <div class="offer-box__title" ng-model="offerHeadline"><span>{{ offer.offerProduct }}</span></div>
              </div>
              <div class="offer-box__content">
                  <div class="offer-box__description">
                      <p>{{ offer.offerDetails }}</p>
                  </div>
                  <div class="offer-box__buttons">
                      <div class="btn btn-success" data="SPRZ" ng-click="sell( offer )" ng-model="sell">sprzedaj</div>
                      <div class="btn btn-info" data-key="ZAPL" ng-click="plan( offer )" ng-model="plan">zaplanuj</div>
                      <div class="btn btn-danger" data="ODRZ" ng-click="drop( offer )"  ng-model="drop">odrzuć</div>
                  </div>
              </div>
          </div>

Controller:

var myApp = angular.module( 'myApp', [] );

myApp.controller( 'AppCtrl', [ '$scope', '$http', '$attrs', function ( $scope, $http, $attrs ) {
  console.log( 'Hello from controller!' );

$scope.proceed = function () {
    var selectedOption = $scope.selectedOption;

    console.log( 'Count fn() clicked' );
    $http.get( '/xxxx/memcached' ).success( function ( response ) {
      if (response) {
        console.log( 'You are in context, good luck! ' + '\n CIF: ' + response + '\n Selected val: ' + $scope.selectedOption );
        $http.get( '/xxxx/offers/' + selectedOption ).success( function ( response ) {
          $scope.offers = response;
          console.log( 'Got offers data I requested:\n' + response );
        } );
      }
      else {
        console.log( 'Please pick up a context!' );
      }
    } )
  };

$scope.plan = function( offer ) {

    console.log(  );      <-- Here I wannt to console.log value from clicked middle button.
  };
2
  • Try to remove ng-model "plan" from: <div class="btn btn-info" data-key="ZAPL" ng-click="plan( offer )" ng-model="plan">zaplanuj</div> Commented Jun 16, 2016 at 8:15
  • After remove shout I use attrs() again? I'v tried jQuery method but it is not ok i guess: Commented Jun 16, 2016 at 8:17

2 Answers 2

1

you can pass arg like this:

<div class="btn btn-success" data="SPRZ" ng-click="sell( offer,$event )" ng-model="sell">sprzedaj</div>

In Js:

$scope.plan = function( offer,id ) {
    console.log( id.target.attributes.data.value );      
  };
Sign up to request clarification or add additional context in comments.

Comments

0

I think in your code you are using ng-model and ng-click using the same name..

<div class="btn btn-info" data-key="ZAPL" ng-click="plan( offer )" ng-model="plan">zaplanuj</div>

Instead you can remove ng-model and try without changing anything on the controller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.