简体   繁体   English

在打开的Angular UI Bootstrap Modal中更新父控制器作用域时,该函数仅运行一次

[英]The function runs only once when updating parent controller scope in opened Angular UI Bootstrap Modal

I have a problem after refactoring code of Test Application backend. 重构测试应用程序后端的代码后,我遇到了问题。 Test has questions and answers, each question has multiple answers and only one of them is correct. 测试有问题和答案,每个问题有多个答案,只有其中一个是正确的。

After some debugging everything seems to work fine, but all questions and answers managing logic was located in one controller. 经过一些调试后,一切似乎都可以正常工作,但是所有的问题和答案管理逻辑都位于一个控制器中。 I moved answers logic to separate nested controller and after that stuck with this problem. 我将答案逻辑移到了单独的嵌套控制器上,之后又陷入了这个问题。 Tried different methods but none of them helped. 尝试了不同的方法,但没有一个有帮助。

The goal is to combine Angular UI Tree (Example #4 - Groups & Categories) with Angular UI Bootstrap Modal. 目标是将Angular UI树(示例4-组和类别)与Angular UI Bootstrap Modal结合起来。

By design adding and editing questions and answers are happening in modal window. 通过设计,可以在模态窗口中添加和编辑问题和答案。 The template is located separately so it's not repeated with each question and answer. 该模板是单独放置的,因此不会在每个问题和答案上重复。 When provided data is not valid, validation errors are shown, otherwise the success alert is shown and all fields clear and back to default state (all that happens inside the modal window, it's stay opened and can be closed only by hitting "Cancel", X button or keyboard Esc button). 如果提供的数据无效,则会显示验证错误,否则会显示成功警报,并且所有字段都将清除并恢复为默认状态(所有发生在模态窗口内的状态,它将保持打开状态,并且只能通过单击“取消”来关闭, X按钮或键盘的Esc按钮)。

I need to update QuestionsCtrl $scope.questions from modal window (trigger button is located inside template of modal window). 我需要从模式窗口中更新QuestionsCtrl $scope.questions (触发按钮位于模式窗口模板内)。 After hitting "Add" button $scope.questions will become updated in scope and the ui.tree is also updated in background. 点击“添加”按钮后, $scope.questions将更新,ui.tree的背景也将更新。 The success alert or validation errors doesn't shown, form fields doesn't clear. 不显示成功警报或验证错误,也不清除表单字段。 Repeated clicks is not calling the actual function again (so it runs only once). 重复单击不会再次调用实际函数(因此它仅运行一次)。

Without modal window but with nested controllers adding works fine. 没有模态窗口,但有嵌套的控制器,添加效果很好。

I created plunk and removed all redundant code so focusing on the problem will be easier. 我创建了插件,并删除了所有冗余代码,因此专注于该问题将更加容易。 I can provide more code if it's needed. 如果需要,我可以提供更多代码。 For example in the plunk results of console.log shown only once. 例如,在console.log的压缩结果中仅显示一次。

It's not that it's not working. 不是说它不起作用。 But you're setting data wrong. 但是您将数据设置错误。 When you write: 当你写:

$scope.add = function() {
    console.log('!!!');
    var data = [{"dataType":"question","id":2,"name":"1. Question 1","answers":[{"dataType":"answer","id":7,"name":"1. Answer 1","correct":false},{"dataType":"answer","id":8,"name":"2. Answer 2","correct":false},{"dataType":"answer","id":9,"name":"3. Answer 3","correct":false}]}];;
    $scope.setQuestions(data);
  };

and: 和:

 $scope.setQuestions = function(data) {
    $scope.questions = data;
  };

you're forcing the questions collection to be data instead of adding new items. 您将问题集合强制为数据,而不是添加新项。 So it's like initializing the questions array all over again. 因此,就像重新初始化问题数组一样。 For this to work you need to use this kind setQuestions function: 为此,您需要使用这种setQuestions函数:

$scope.setQuestions = function(data) {
    $scope.questions.push(data);
  };

that way your new question will be added to the already existing ones. 这样,您的新问题将被添加到已经存在的问题中。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM