无法使用离子中的ng-model从模态中检索值

时间:2015-07-17 10:52:52

标签: angularjs cordova ionic

  

这是我的modal-profile.html

 <ion-modal-view>
        <form name="itemEdit" novalidate>
            <ion-header-bar class="bar-positive fix-buttons">
              <div class="row">
              <a class="button  " ng-click="closeModal()">Cancel</a>
              <h1 class="title ">Diary</h1>
            </div>
            </ion-header-bar>
            <ion-content has-bouncing="true">
              <div class="row" ng-class="{'no-padding-top': !data.editItem}">
                <div class="col">
                  <label class="item item-input large">
                    <input type="text" placeholder="Title" ng-model="modelTitle">
                  </label>
                </div>
              </div>

             <div class="row description-row">
                <div class="col">
                  <label class="item item-input text">
                    <textarea placeholder="Description" rows="5" ng-model="modelDescription" ng-Required="true" lose-focus-on-return></textarea>
                  </label>
                </div>
              </div>


               <button class="button" ng-click="addDetail()">ADD</button>

              <div class="row charity-row">
                <div class="col col-10 vert-center">
                  <div class="charity large"></div>
                </div>
              </div>


            </ion-content>
          </form>
      </ion-modal-view>
  

这是我的控制器

.controller('ChatsCtrl', function($scope,$ionicModal, Chats,$state,$cordovaSQLite) {

    $scope.chats = Chats.all();
    $scope.remove = function(chat) {
        Chats.remove(chat);
    }

    $ionicModal.fromTemplateUrl('templates/modal-profile.html', {
       scope: $scope,
       animation: 'slide-in-up'
    }).then(function(modal){
       $scope.modal = modal
    })

    $scope.openModal = function(){          
      $scope.modal.show();
    }

    $scope.addDetail = function(){

        alert($scope.modelTitle);
        alert($scope.modelDescription);

        var db = $cordovaSQLite.openDB("diary.db");
        var query = "INSERT INTO details (title, description) VALUES (?,?)";

        $cordovaSQLite
            .execute(db, query, [$scope.modelTitle, $scope.modelDescription])
            .then(function(res){
                alert("success");
            }, function(err){
                alert(err);
            });
    }
})
  

我想将modelTitle,modelDescription参数传递给&#39; ChatCtrl&#39;控制器。所以我在addDetail()函数中添加两个警告来检查。但那些没有显示任何值。任何人都可以帮助我吗?    提前谢谢!

1 个答案:

答案 0 :(得分:11)

直到我使用过,而不是在ng-model中使用字符串尝试使用绑定和检索值的对象。像

这样的东西

HTML:

patindex()

JS:

      <div class="row" ng-class="{'no-padding-top': !data.editItem}">
        <div class="col">
          <label class="item item-input large">
            <input type="text" placeholder="Title" ng-model="obj.modelTitle">
          </label>
        </div>
      </div>

     <div class="row description-row">
        <div class="col">
          <label class="item item-input text">
            <textarea placeholder="Description" rows="5" ng-model="obj.modelDescription" ng-Required="true" lose-focus-on-return></textarea>
          </label>
        </div>
      </div>


       <button class="button" ng-click="addDetail(obj)">ADD</button>

我使用堆栈参考link here

得到了这个