[英]AngularJS Bootstrap UI, issues passing data into modal. selectedCards undefined
I have a feeling I am very much confused about the $scope, and am therefore selecting nothing. 我对$ scope感到非常困惑,因此什么也没选择。 Having a hard time understanding where selectedCard is being called from, if at all. 很难理解从何处调用selectedCard。 Would anyone mind explaining to me where I am going wrong? 有人介意向我解释我要去哪里哪里吗?
NOTE: I believe I have too many files etc to create a plunkr worth looking through, but a live version of the site can be found here: http://marcmurray.net/test_sites/card_site/index.html 注意:我相信我有太多文件等,无法创建一个值得一看的插件,但是可以在这里找到该站点的实时版本: http : //marcmurray.net/test_sites/card_site/index.html
Javascript: 使用Javascript:
angular.module("cardSite", ['masonry', 'ngAnimate', 'ui.bootstrap'])
.controller('mainCtrl', function($scope, dataService, $scope, $uibModal, $log) {
$scope.cards = [];
dataService.getCards().then(function(response) {
$scope.cards = response.data;
}, function() {});
$scope.plusOne = function(card) {
card.card_rating += 1;
};
$scope.isActive = false;
$scope.changeClass = function() {
$scope.isActive = !$scope.isActive;
};
// Modal stuff here
$scope.modalUpdate = function(size, selectedCard) {
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'sendCard.html',
controller: function($scope, $uibModalInstance, cards) {
$scope.cards = cards;
$scope.send = function() {
$uibModalInstance.close($scope.selectedCard);
console.log($scope.selectedCard);
};
$scope.cancel = function() {
$uibModalInstance.dismiss('cancel');
};
},
size: size,
resolve: {
cards: function() {
return $scope.selectedCard;
}
}
});
modalInstance.result.then(function(selectedCard) {
$scope.selected = selectedCard;
}, function() {
$log.info('Modal dismissed at: ' + new Date());
});
};
});
sendCard.html: sendCard.html:
<div ng-controller="mainCtrl">
<div class="modal-header">
<h3 class="modal-title">Ready to send a card?</h3>
</div>
<div class="modal-body">
Card selected: <b>{{card.card_title}}</b>
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" ng-click="send()">Send</button>
<button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
</div>
HTML for calling the modal: 用于调用模式的HTML:
<button ng-click="modalUpdate(lg, card)" class="btn btn-stretch">Send</button>
Try to remove the: 尝试删除:
ng-controller="mainCtrl"
In your sendCard.html file 在您的sendCard.html文件中
There's no need to use 不用用了
$scope.selectedCard
You can just use selectedCard, since it's not tied to the $scope anyway: 您可以只使用selectedCard,因为它与$ scope无关:
resolve: {
cards: function() {
return selectedCard;
}
}
You don't actually have selectedCard
set on your main controller's scope; 您实际上没有在主控制器的作用域上设置selectedCard
; you are just passing in whichever card is clicked as the selectedCard
. 您只是传递了单击的任何卡作为selectedCard
。 Thus, all you have to do is resolve the card the user has clicked: 因此,您要做的就是解析用户单击的卡片:
resolve: {
card: function() {
return selectedCard;
}
}
...And pass in card
to your modal controller function. ...然后将card
传递给模态控制器功能。 Then, assuming your selectedCard
has the property you show in your template ( card_title
), everything should render as you expect. 然后,假设您的selectedCard
具有您在模板中显示的属性( card_title
),则所有内容都应按预期呈现。
EDIT: The above will work for you, but also just noticed that you have the plural $scope.cards
in your modal controller and singular card
in your template. 编辑:以上将为您工作,但也只是注意到您在模态控制器中有复数$scope.cards
,在模板中有单数card
。 You might consider changing everything to the singular, since it matches your data object. 您可能会考虑将所有内容更改为单数,因为它与您的数据对象匹配。 It's also hard to tell if your sendCard.html
is in a separate file (or is an ng-template script), but as @eliagentili said, you should remove the ng-controller attribute from it. 很难说出sendCard.html
是否在单独的文件中(或者是ng-template脚本),但是正如@eliagentili所说,您应该从中删除ng-controller属性。
Finally, you'll need to fix your send
function: 最后,您需要修复send
功能:
$scope.send = function() {
$uibModalInstance.close($scope.card);
console.log($scope.card);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.