我遇到从自定义数据属性获取值到控制器的问题。我想从data-key属性中调用console.log值。
我试过使用$ attrs但不知道,请提示或建议。
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>
控制器:
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.
};
答案 0 :(得分:1)
你可以像这样传递arg:
<div class="btn btn-success" data="SPRZ" ng-click="sell( offer,$event )" ng-model="sell">sprzedaj</div>
在Js:
$scope.plan = function( offer,id ) {
console.log( id.target.attributes.data.value );
};
答案 1 :(得分:0)
我认为在您的代码中,您使用的是ng-model和ng-click,使用相同的名称..
<div class="btn btn-info" data-key="ZAPL" ng-click="plan( offer )" ng-model="plan">zaplanuj</div>
相反,你可以删除ng-model并尝试不改变控制器上的任何内容。