【问题标题】:angularjs dialog form data not updating scopeangularjs对话框表单数据不更新范围
【发布时间】:2018-01-04 13:13:29
【问题描述】:

我正在创建一个小应用程序,我在其中打开一个角度材料对话框来编辑客户地址信息。当前对话框打开并显示现有地址信息,当对话框中的数据被编辑和提交时,数据库会更新。但是,页面上的地址信息不会自行更新。

数据库更新后,页面上的 Add1 和 Add2 应更新为新信息。我不知道该怎么做,请帮忙?

我不希望主页在用户输入地址时更新,只有在 exec 函数完成时才更新。

HTML:

        <div ng-controller="AppCtrl" class="md-padding dialogdemoBasicUsage" id="popupContainer" ng-cloak="" ng-app="MyApp">


          <div class="dialog-demo-content" layout="row" ayout="row" layout-wrap="" layout-margin="" layout-align="center">
            <p>Add1: {{ customerdetails.Add1 }}</p>
            <p>Add2: {{ customerdetails.Add2 }}</p>
            <br/>
            <md-button class="md-primary md-raised" ng-click="showTabDialog($event)">
              Open Dialog
            </md-button>
          </div>


          <script type="text/ng-template" id="tabDialog.tmpl.html"><md-dialog aria-label="Mango (Fruit)">
          <form>
            <md-toolbar>
              <div class="md-toolbar-tools">

                <span flex></span>
                <md-button class="md-icon-button" ng-click="cancel()">
                  <md-icon md-svg-src="img/icons/ic_close_24px.svg" aria-label="Close dialog"></md-icon>
                </md-button>
              </div>
            </md-toolbar>
            <md-dialog-content style="max-width:800px;max-height:810px; ">
              <md-tabs md-dynamic-height md-border-bottom>
                <md-tab label="one">
                  <md-content class="md-padding">
                    <md-input-container class="md-block">
                     <label>Address 1</label>
                      <input ng-model="customerdetails.Add1">
                    </md-input-container>
                    <md-input-container class="md-block">
                       <label>Address 2</label>
                       <input ng-model="customerdetails.Add2">
                    </md-input-container>
                  </md-content>
                </md-tab>

              </md-tabs>
            </md-dialog-content>

            <md-dialog-actions layout="row">
              <md-button ng-click="editAddressSubmit()" style="margin-right:20px;" >
                Update
              </md-button>
            </md-dialog-actions>
          </form>
        </md-dialog>
        </script>
        </div>

JS

angular.module('MyApp',['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])

        .controller('AppCtrl', function($scope, $mdDialog, $mdMedia) {
          $scope.customerdetails={
            Add1: 'line 1',
            Add2: 'line 2'
          }

          $scope.showTabDialog = function(ev) {
            $mdDialog.show({
              controller: 'AppCtrl',
              templateUrl: 'tabDialog.tmpl.html',
              parent: angular.element(document.body),
              targetEvent: ev,
              clickOutsideToClose:true
            })

          };

          $scope.cancel = function() {
            $mdDialog.cancel();
          };

          $scope.editAddressSubmit = function() {
             console.log('edit address submit funtion clicked')
             //in live code the database is updated here but the Add1 and Add2 is not updated on the main page once the dialog is closed!
             return $scope.exec('call/UpdateCustomerSQL',
                 {
                   Add1: $scope.customerdetails.Add1,
                   Add2: $scope.customerdetails.Add2
               }
             ).then(function() { // close popup
                 console.log('then function close popup')
                 $mdDialog.cancel();
              })
             }
        }) //close controller

【问题讨论】:

    标签: javascript angularjs angularjs-scope angularjs-material


    【解决方案1】:

    诀窍是您必须传入一个隔离的scope,这样您的对话框的范围就不会绑定到您的父范围。为此,您必须使用参数local,并使用不同的控制器来处理逻辑。

    $mdDialog.show({
       controller: 'DialogCtrl',
       templateUrl: 'tabDialog.tmpl.html',
       parent: angular.element(document.body),
       targetEvent: ev,
       clickOutsideToClose: true,
       locals: {
         address1: $scope.customerdetails.Add1,
         address2: $scope.customerdetails.Add2,
       }
    })
    

    然后,您将使用另一个控制器DialogCtrl 来处理对话框中所需的任何逻辑。在这里,您可以将address1address2 注入到您的控制器中,类似这条线。

    .controller('DialogCtrl',function($scope,$mdDialog, address1, address2){
       console.log(address1, address2) //this will have the values as $scope.customerdetails.Add1 and 2
    }
    

    现在您已经成功地将数据从您的AppCtrl 传递到您的DialogCtrl,然后您可以执行您的数据库事务。之后,您将必须解决承诺,以便您的 AppCtrl 可以获取更新的数据。

    我叉了你的笔:http://codepen.io/cozyazure/pen/PzYzQd

    我没有你的服务,所以我嘲笑了$timeout来模仿承诺回报

    【讨论】:

    • 谢谢,这很好!我可以在本地传递整个 customerdetails 对象吗?因为实际上有很多变量,而不仅仅是 2。谢谢
    • @Janey 让我知道结果,如果它解决了您的问题(或至少指向正确的方向),请标记已接受的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多