2015-09-17 126 views
7

我有使用指令的角度应用程序。 在指令中我有定义弹出模式的模板。当输入数据发生变化时,AngularJs不更新模型

基本上,这是非常简单的应用程序,显示本书作者的列表,然后在列表中有打开模式对话框编辑按钮。 如果我打开编辑书籍作者的模式,并关闭它,而不编辑作者 - 这是没有问题的。

但是,如果我打开模式,然后在作者输入中键入内容并关闭它,那么模型会一直停留在当前输入值,所以如果我打开另一个作者进行编辑,模型将不会被更新。

我的问题是:为什么会发生这种情况,以及如何解决它?

HTML

<div ng-controller="MyCtrl"> 
    <table class="table table-hover"> 
      <tr>    
       <td><b>Publisher</b></td>     
       <td><b>Edit Publisher</b></td> 
      </tr> 
      <tr ng-repeat="book in books"> 
       <td> 
        {{book.Author}} 
       </td> 
       <td> 
        <span ng-click="toggleModal(book)" class="btn btn-primary">Edit</span> 
       </td> 
      </tr> 
     </table> 

    <modal-dialog info='modalShown' show='modalShown' width='600px' height='60%'> 
     <div ng-show="divBook"> 
       <input type="text" name="bookAuthor" ng-model="bookAuthor" /> 
     </div> 
    </modal-dialog> 
</div> 

var myApp = angular.module('myApp',[]); 

myApp.controller("MyCtrl", function($scope){ 
    $scope.books = [{Author:"Jon Skeet"},{Author:"John Papa"},{Author:"Scott Hanselman"}]; 

    $scope.modalShown = false; 
    $scope.toggleModal = function (book) { 
      $scope.bookAuthor = book.Author; 
      $scope.modalShown = !$scope.modalShown; 
      $scope.divBook = true; 
    };  
}); 

myApp.directive('modalDialog', function() { 
    return { 
     restrict: 'E', 
     template: "<div class='ng-modal' ng-show='show'>" 
        +"<div class='ng-modal-overlay' ng-click='hideModal()'>" 
        +"</div>" 
         +"<div class='ng-modal-dialog' ng-style='dialogStyle'>" 
         +"<div class='ng-modal-close' ng-click='hideModal()'>X</div>" 
         +"<div class='ng-modal-dialog-content' ng-transclude>" 
        +"</div>" 
        +"</div>" 
       +"div>", 
     replace: true, 
     scope: { 
      show: '=info' 
     }, 
     transclude: true, 
     link: function (scope, element, attrs) { 

      //scope.apply(); 
      scope.dialogStyle = {}; 
      if (attrs.width) 
       scope.dialogStyle.width = attrs.width; 
      if (attrs.height) 
       scope.dialogStyle.height = attrs.height; 
      scope.hideModal = function() { 
       scope.show = false; 
      }; 
     } 
    }; 
}); 

因此,测试案例是:

点击编辑 - >更改值 - >关闭模式

单击编辑另一位作者。

的jsfiddle:http://jsfiddle.net/HB7LU/17694/

回答

0

模型值是变化的,然而要创建一个新的变量,而不是修改阵列的原始元素。

您可以通过将所述阵列的对象的指针在变量

$scope.toggleModal = function (book) { 
     $scope.book = book; 
     $scope.modalShown = !$scope.modalShown; 
     $scope.divBook = true; 
}; 

然后指向NG-模型提供给对象的属性.Author一个范围改变这种状况。

<input type="text" name="bookAuthor" ng-model="book.Author" /> 

查看修改的jsfiddle:http://jsfiddle.net/9a2jcc9u/1/

0

我已经改变了你的JS提琴,如果要更改名称,并将其在电网自动改变比去除angular.copy(书),并直接指定的书。你可以在这里看到你的jsfiddle jsfiddle

myApp.controller("MyCtrl", function($scope){ 
    $scope.books = [{Author:"Jon Skeet"},{Author:"John Papa"},{Author:"Scott Hanselman"}]; 

    $scope.modalShown = false; 
    $scope.toggleModal = function (book) { 
      $scope.book = angular.copy(book); 
      $scope.modalShown = !$scope.modalShown; 
      $scope.divBook = true; 
    };  
}); 

您的模态对话框

<modal-dialog info='modalShown' show='modalShown' width='600px' height='60%'> 
    <div ng-show="divBook"> 
      <input type="text" name="bookAuthor" ng-model="book.Author" /> 
    </div> 
</modal-dialog> 
0

尝试这样的事情

myApp.controller('MyCtrl', ['$scope',function($scope) { 

//your code 

}]); 
相关问题