如何在angular ui模态控制器中定义函数
How to define a function inside angular ui modal controller
我试图在angular ui模态控制器中定义一个函数,默认情况下a找到了两个函数$scope.ok和$scope.cancel,我想添加我的函数,从发送到该控制器的项目列表中删除一个项目这是我的角度ui模态控制器代码:
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);
};
});
在这里,我将项目列表发送到模式控制器
$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/了解更多信息。
相关文章:
- 当显式定义控制器参数时,默认模型绑定器会发生异常
- $routeParams在传递到新视图时未定义&控制器
- Http服务工厂将未定义的返回到Angular中的控制器
- 角度指令控制器:参数不是函数,未定义
- 注入应用程序的角度控制器未定义
- 如何正确地将数据从自定义服务传递到角度控制器
- 如何在angular ui模态控制器中定义函数
- AngularJS:如何调用指令中定义的函数'控制器的作用域
- 未定义的控制器AngularJS
- 如何在来自另一个文件的控制器中定义变量
- 在自定义角度指令中使用控制器
- 使用 ng-model 定义变量之间的区别,而不是通过 AngularJS 中的控制器定义变量
- 为什么我的AngularJS控制器定义和标记没有按预期工作?-<控制器>不是函数错误
- 测试Angular控制器定义为Angular .module('myApp').controller(.
- 在es6类中为angular 1.5控制器定义常量
- 以下agularjs控制器定义的区别是什么?
- AngularJS:浏览器忽略我的控制器定义(依赖注入)
- 如何在angularjs中从app.js内部的控制器定义变量
- AngularJS指令控制器定义
- 将控制器定义为通用对象而不是函数是不是一种糟糕的做法