I want to create a button action create with disable/enable button then close model view: // button: type="submit" class="btn btn-primary" data-dismiss="modal" ng-disabled="!newSong.name" ng-click="saveSong()" >Create --> Issue: when ng-click done --> view modal did not close
Asked
Active
Viewed 396 times
-2
-
Please post code :) – mayank bisht Dec 22 '17 at 10:37
-
i wan to create 1 button some thing like this: action: did not close view modal – FanLink Dec 22 '17 at 11:07
-
Please provide code in the question, with the current progress of your code, I cannot write entire code from the scratch :) – mayank bisht Dec 22 '17 at 11:21
-
Thank you! this is sample mycode :https://codepen.io/FanLink/pen/zpKaoW?editors=1010 – FanLink Dec 22 '17 at 11:50
1 Answers
0
The reason why your code was not working is because of $scope.newSong = {};
in this code:
$scope.saveSong = function () {
$scope.songs.push($scope.newSong);
$scope.newSong = {};
};
Whenever you are trying to submit the form $scope.newSong = {}
is making it invalid.
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">×</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/>
Angular CODE
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);
}
});
Hope that works :)

mayank bisht
- 784
- 9
- 19
-
Thank mayank bisht so much! it works :). Thank you for your consideration. – FanLink Dec 22 '17 at 16:57
-
But one more problem : when you click again button Add new , the previous value input did not clear. – FanLink Dec 22 '17 at 17:26
-
Check this link `https://codepen.io/anon/pen/opzmem?editors=1010`, IT will clear older inputs – mayank bisht Dec 22 '17 at 17:50
-