ng模型未在控制器中更新

时间:2014-12-03 16:44:19

标签: angularjs angularjs-directive modal-dialog

我有一个从指令启动的模态(模态包含一个选择选项)
我无法将ngModel更新到我的控制器中 任何帮助将不胜感激

非常感谢

的test.html

<div class="col-md-5">
               <select ng-model="noteSelected">
                      <option ng-selected="{{note == noteSelected}}" value="{{note }}" ng-repeat="note  in noteList">{{note }}</option>
               </select>           
               <button id="buttonOk" class="btn btn-primary" ng-click="ok()">OK</button>
</div>

指令

link : function(scope, element, attr) {             
                    scope.openDialog = function() {                 
                            var modalInstance = $modal.open({
                                templateUrl : 'partials/template/test.html',
                                controller : 'ModalInstance',
                                resolve : {
                                    infos: function() {
                                        return scope.infos;                                 
                                }
                            });

模态

cmcApp.controller('ModalInstance', [
        "$scope", "$modalInstance", "infos", 
        function($scope, $modalInstance, infos) {

$scope.noteList= infos.noteList;
$scope.ok = function() {
$scope.newNote = $scope.noteSelected ; //always undefined
          };
     }
]);

1 个答案:

答案 0 :(得分:1)

当你将$ scope.noteSelected指定给$ scope.newNote时,它是未定义的,你实际上似乎没有更新它。

试试这个:

为您的选择添加更改:

HTML:

<select ng-model="noteSelected" ng-change="updateNewNote()">

并在您的控制器中指定更改值的函数:

$scope.updateNewNote = function() {
  $scope.newNote = $scope.noteSelected ;
};

或在控制器中添加一个手表,在更改时将新值分配给$ scope.newNote

$scope.$watch('noteSelected', function(selected) {
    $scope.newNote = selected
  });