gpt4 book ai didi

javascript - 如何在angular ui modal controller中定义一个函数

转载 作者:行者123 更新时间:2023-11-30 16:06:22 26 4
gpt4 key购买 nike

我尝试在默认情况下在 Angular UI 模式 Controller 中定义一个函数,找到两个函数 $scope.ok 和 $scope.cancel 我想添加我的函数,从发送的项目列表中删除一个项目到那个 Controller 这是我的 Angular ui 模式 Controller 代码:

myapp.controller('ModalInstanceCtrl', function ($scope,$location,$uibModalInstance, items) {

$scope.items = items;
$scope.selected = {
item: $scope.items[0]
};

$scope.ok = function () {
$uibModalInstance.close($scope.selected.item);
alert("redirection");
$location.path('/questionnaire');
};
$scope.closeListeChoix = function () {
$uibModalInstance.close($scope.selected.item);

};
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
$scope.deleteChoix=function($index)
{

alert("supp")
$scope.items.splice($index, 1);
};
});

在这里我将项目列表发送给模态 Controller

$scope.ListeChoixOneQuestion=[];
$scope.openListeChoix = function ($index) {
$scope.ListeChoixOneQuestion=$scope.questions[$index].choix ;
console.log("*********** choix de la question **********")
for(var i=0;i<$scope.ListeChoixOneQuestion.length;i++){
console.log("choix : "+$scope.ListeChoixOneQuestion[i].designation);
}
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'listeOfChoix.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
return $scope.ListeChoixOneQuestion;
}
}
});

这是我的 html 代码,当我在我的 ng-click 中调用函数 deleteChoix 时没有任何反应,并且该项目没有从项目列表中删除任何解决方案

 <div class="modal-body">
<div class="row">
<div class="table-responsive">
<table id="Table2" class="table table-bordered table-striped">
<thead>
<tr>
<th>Designation</th>
<th>Image</th>
<th>Aller à la question</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="choix in items track by $index">
<td>{{choix.designation}}</td>
<td>{{choix.imageUrl}}</td>
<td>{{choix.gotoQuestion}}</td>
<td class="text-center" style="width: 50px;">
<span class="btn btn-danger btn-xs fa fa-remove" style="cursor: pointer;" ng-click="deleteChoix($index);"></span>
</td>
<tr>
</tbody>
</table>
</div>
</div>
</div>

最佳答案

如评论中所述,简短的解决方案是

$parent.deleteChoix($index);

由于 Javascript 中的继承限制,这是一个范围问题。
如果您不想遇到这个问题,请始终使用中间对象,例如:

 $scope.context = {};// NEVER forget to initialize it in your controller or it won't work even if you don't put anything in at the start.
$scope.context.deleteChoix = [...];

因此您无需考虑是否应该使用 $parent 甚至 $parent.$parent

检查 http://zcourts.com/2013/05/31/angularjs-if-you-dont-have-a-dot-youre-doing-it-wrong/获取更多信息。

关于javascript - 如何在angular ui modal controller中定义一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36982259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com