【发布时间】:2014-02-26 11:03:28
【问题描述】:
我正在尝试在我的应用程序中使用 Angularjs-UI 模态,但我对去哪里有点困惑。我有一个用于调用模型的新组的按钮,模型控制器在另一个文件中。我试图在 groupCtrl 中调用 newGroupCtrl 但它返回未定义。
新组按钮的 HTML:
<button type="button" class="delGroupBtn" ng-click="newGroup()">New Group</button>
在我的 groupCtrl 中,我有这个 newgroup() 函数:
$scope.newGroup = function (){
var modalForm = '/Style%20Library/projects/spDash/app/partials/newGroup.html';
var modalInstance = $modal.open({
templateUrl: modalForm,
backdrop: true,
windowClass: 'modal',
controller: newGroupCtrl,
resolve:{
newGroup:function (){
return $scope.newGroup;
}
}
});
};
然后我得到了我的 newGroup.html(模式),用户可以在其中输入组名称、描述、所有者:
<div class="modal-header">
<form>
<label for="groupName">Group Name:
<input id="groupName" type="text" ng-model='newGroup.name'>
</label>
<hr>
<label for="groupDesc">Group Description:
<input id="groupDesc" type="text" ng-model='newGroup.desc'>
</label>
<hr>
<label for="groupOwner">Group Name:
<select id="groupOwner" type="text" ng-model=''></select>
</label>
<div class="modal-footer">
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
<button class="btn primary-btn" type="button" ng-click="newGroup()">Create</button>
</div>
</form>
</div>
这是新的GroupCtrl:
spApp.controller('newGroupCtrl',
function newGroupCtrl($scope, $modalInstance){
$scope.newGroup = {
name:null,
desc:null
};
$scope.submit = function(){
console.log('creating new group');
console.log(newGroup);
$modalInstance.dismiss('cancel');
}
$scope.cancel = function (){
$modalInstance.dismiss('cancel');
};
$modalInstance.result.then(function (){
groupService.newGroup($scope.newGroup);
}, function (){
console.log('No new group created.');
});
}
);
我已经用我的组服务注入了 group 和 newGroup 控制器,这是我试图从模型获取信息到 groupService 函数的地方,以便对我的服务器进行 AJAX 调用并创建新组。似乎我在使用 $model.open({}) 的两个控制器中重复自己
这是plunker
【问题讨论】:
-
与angular-ui.github.io/bootstrap/#/modal 的文档相比,它看起来不错。所以它可能在实施中。你的笨蛋在那里没有帮助。请重构它以运行或重现您在测试环境中看到的相同结果。
-
不确定这是否是您的主要问题,但 modalInstance.result.then 应该在 groupCtrl 中,而不是 newGroupCtrl(没有 $)。此外,您可能想在提交回调中调用 close() 而不是 dismiss() 。看起来您正试图从模态控制器内部调用父控制器方法。您要做的是通过 $modalInstance.close() 从模态中传回数据,然后在解析 modalInstance.result 时让父控制器对该数据进行操作。
-
你的意思是这样的:jsfiddle.net/Qqmk5 我也做了一些改动。我没有为 newGroupCtrl 创建一个单独的文件,而是在模态的控制器参数中写出了函数。我不知道如何在控制器中引用控制器 js 文件。现在模态实际上显示了哪个好,但我仍在尝试弄清楚如何将模态数据传递给我的服务。
标签: javascript angularjs angular-ui angularjs-module