我对按钮/div 中的自定义数据键属性的 POST 数据有疑问。 应用程序,加载 data-key 属性的 GET 值后。单击一个按钮后,按钮将使用该属性的 POST 值创建一个函数。
问题是我无法正确设置 $scope,无法从单击的按钮以 JSON 格式传递正确的值(应用程序有 1-3 个可单击的按钮,每个按钮都有不同的值)。
请提供提示或建议,致以诚挚的问候,
Controller :
var myApp = angular.module( 'crm', [] );
myApp.controller( 'AppCtrl', [ '$scope', '$http', function ( $scope, $http ) {
console.log( 'Hello from controller!' );
$http.get( '/xxx/dictionary' ).success( function (response) {
$scope.items = response;
console.log( 'I got data I requested: \n' + response);
$scope.proceed();
} );
$scope.proceed = function () {
var selectedOption = $scope.selectedOption;
console.log( 'Count fn() clicked' );
$http.get( '/xxxx/xxxx' ).success( function ( response ) {
if (response) {
console.log( 'You are in context, good luck! ' + '\n CI: ' + response + '\n Selected val: ' + $scope.selectedOption );
$http.get( '/xxx/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.sell = function() {
console.log( 'Sell clicked!' );
$http.post( '/response' )...? <--- Here is Problem
};
查看:
<div class="offer" ng-model="offerBox">
<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-key="{{ offer.statusId }}" ng-click="sell()" ng-model="sell">sprzedaj</div>
<div class="btn btn-info" data-key="{{ offer.statusId }}" ng-click="plan()">zaplanuj</div>
<div class="btn btn-danger" data-key="{{ offer.statusId }}" ng-click="drop()">odrzuć</div>
</div>
</div>
</div>
最佳答案
我在 Controller 中创建了function (offer, id)
,并在 View 中创建了sell(offer, $event)
,并且我在 Controller 中获取了值:
var action = id.target.attributes.data.value;
感谢您的提示,尤其是提示sell(offer)
问候!
关于javascript - 在 ng-click 后,在 AngularJS Controller 内以 JSON 格式从 html 属性 data-kay 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37845117/