Angular UI Bootstrap Modal update $scope

后端 未结 1 1905
醉话见心
醉话见心 2021-02-04 13:28

I want to use the modal to edit my data. I pass the data to the modal instance. When I click ok I pass the edited data in the $scope.selected back to the controller.

Th

1条回答
  •  隐瞒了意图╮
    2021-02-04 14:14

    You didn't include your template for the modal, so this is a bit of a guess. Your code is pretty close to the example code for the angular-ui modal, which uses ng-repeat in the template. If you're doing the same thing, then you should be aware that ng-repeat creates a child scope which inherits from the parent.

    Judging from this snippet:

    $scope.ok = function () {
        $modalInstance.close($scope.selected);
    };
    

    it looks like instead of doing this in your template:

  • {{ item }}
  • you may be doing something like this:

  • {{ item }}
  • If so, then in your case, you're assigning selected in the child scope, and this will not affect the parent scope's selected property. Then, when you try to access $scope.selected.name, it will be empty. In general, you should use objects for your models, and set properties on them, not assign new values directly.

    This part of the documentation explains the scope problem in more detail.

    Edit:

    You are also not binding your inputs to any model at all, so the data you enter there is never stored anywhere. You need to use ng-model to do that, e.g.:

    
    
    

    See this plunkr for a working example.

    0 讨论(0)
提交回复
热议问题