0

我是角度材料的新手,我的问题是我无法为第二次打开对话框绑定范围数据。

我从分配给范围变量的数据库中检索数据,并在 md 对话框中绑定了范围 var。(对于第一个 tym,它可以正常工作,没有问题)在第二个不能将数据与 md 对话框绑定

下面是我的代码示例

  var retrieveData = function(){
    var companyUrl =  dbUrl; 
     var request = {
         url:  companyUrl,
           type: "GET",

      };
    $http(request)
            .success(function (retrievedSource){

    // here I am assigning retrieved data to the scope variable 
                 $scope.retrievedSources = retrievedSource;

                }).error(function (){
                 console.log("search failed");
            });
 };



 <md-input-container class="md-icon-float md-block">
<label>Company Name  <span> * </span></label>
<input type="text" ng-required="true" style="width:100%;" name="companyName"  ng-pattern="namePattern" value='{{retrievedSources.companyName}}' />
<span id="companyNameErrorMessages" class="error"></span>

问题:

value='{{retrievedSources.companyName}}'

使用 mdDialog cancel 取消对话框后,此范围分配第一次正常工作我无法绑定范围数据

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

1 回答 1

1

我创建了一个CodePen,其中包含一个$scope.reterivedSourcesmd-dialog.

新值按钮更新$scope.reterivedSources.companyName。我不知道您如何设置代码以显示对话框,但请在我的代码中注意这一点:

scope: $scope,
preserveScope: true

JS

.controller('AppCtrl', function($scope, $mdDialog) {
  $scope.retrievedSources = { companyName: 123 };

  $scope.showDialog = function(ev ,id) {
   $mdDialog.show({
     templateUrl: "test.html",
     parent: angular.element(document.body),
     clickOutsideToClose:true,
     targetEvent: ev,
     controller: materialEditCtrl,
     fullscreen: false,
     scope: $scope,
     preserveScope: true
   });
  };

  $scope.newValue = function () {
    $scope.retrievedSources.companyName = Math.random() * 1000;
  }
});

标记

<div ng-controller="AppCtrl" ng-cloak="" class="buttondemoBasicUsage" ng-app="MyApp">
  <script type="text/ng-template" id="test.html">
    <md-dialog>
            <md-input-container class="md-icon-float md-block">
            <label>Company Name  <span> * </span></label>
            <input type="text" ng-required="true" style="width:100%;" name="companyName"  ng-pattern="namePattern" value='{{retrievedSources.companyName}}' />
            <span id="companyNameErrorMessages" class="error"></span>
    </md-dialog>
  </script>

  <md-button ng-click="showDialog($event)" class="md-raised md-primary">
    edit
  </md-button>
  <md-button ng-click="newValue()" class="md-raised md-primary">
    new value
  </md-button>
</div>
于 2016-07-12T12:21:50.583 回答