解除模态视图问题

时间:2017-12-22 10:35:21

标签: html angularjs bootstrap-grid

我想用禁用/启用按钮创建一个按钮动作创建,然后关闭模型视图: //按钮:type ="提交" class =" btn btn-primary"数据驳回="模态" NG-禁用="!newSong.name"纳克单击=" saveSong()" >创建 - >问题:当ng-click完成时 - >查看模态没有关闭

1 个答案:

答案 0 :(得分:0)

您的代码无效的原因是$scope.newSong = {};
在此代码中:

$scope.saveSong = function () {
        $scope.songs.push($scope.newSong);
        $scope.newSong = {};
    };

每当您尝试提交表单时,$scope.newSong = {}都会使其无效。

HTML CODE

<div ng-app="myApp", ng-controller ="myController">
<div ng-repeat="id in songs">
      <ul>
        <li> Name song: {{id.name}} </li>
        <li> Name artist: {{id.artist}} </li>
      </ul>
  </div>
  <div class="col-lg-1">  <button type="button" class="btn btn-info glyphicon glyphicon-plus" data-toggle="modal" data-target="#myModal" ng-click="clear()">   Add New </button> </div>
  <div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title"> Create New Song</h4>
        </div> 
        <div class="modal-body">
          <form class="form-horizontal" role ="form" name="formName" novalidate>

            <div class="form-group" >
              <label class="control-label col-sm-2"> Name* </label>
              <div class="col-sm-10">
                <input type="text" class="form-control" name="name" ng-model="newSong.name" required/>
                <div class="custom-error" ng-show="formName.name.$dirty && formName.name.$invalid">
                  <p ng-show="formName.name.$invalid && !formName.name.$pristine" class="help-block"> name is required.</p>
                </div>
              </div>
            </div>

            <div class="form-group">
              <label class="control-label col-sm-2">Artist</label>
              <div class="col-sm-10">
                <input type="text" class="form-control"ng-model="newSong.artist">
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="submit" class="btn btn-primary"  data-dismiss="modal" ng-disabled="!newSong.name" ng-click="saveSong()" >Create</button>
          <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        </div>
      </div>
    </div>
  </div>
</div>
<br/><br/>

角色代码

var myApp = angular.module('myApp',[]);
myApp.controller('myController', function($scope){
  $scope.songs = [
  {
    "name": "Everybody",
    "artist": "Backstreet Boys.",
    "id": 1
  },
  {
    "name": "Feel Like Makin' Love",
    "artist": "Bad Company.",
    "id": 2
  },
  {
    "name": "Sister of Pearl",
    "artist": "Baio.",
    "id": 3
  }];
     $scope.newSong = {};

    /* callback ng-click create */
    $scope.saveSong = function () {
        $scope.songs.push($scope.newSong);
       // $scope.newSong = {};
    };
  $scope.clear = function(){

  $scope.newSong = {};
    $scope.$setPristine(true);
  }
});



WORKING LINK

希望有效:)