html - Toggle button does not work -
i trying implement functionality trying save details of contacts after clicking on edit button. once click on edit button labels should convert editable text fields , save button should appear. once click on edit button save button indeed appears when click on save button nothing happens. should change edit button. not sure making mistake. using json-server rest.
this html file:-
<!doctype html> <html data-ng-app="mycontactapp" > <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/y6pd6fv/vv2hjna6t+vslu6fwyxjcftcephbnj0lyafsxtsjbbfadjzaleqsn6m" crossorigin="anonymous"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rhyon1irsvxv4nd0jutlngaslcjuc7uwjduw9svrlvryoopp2bwygmgjqixwl/sp" crossorigin="anonymous"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script> <script src="app.js"></script> <script src="appconfig.service.js"></script> <script src="appname.service.js"></script> <script src="contact.controller.js"></script> <script src="contactdata.service.js"></script> <title>my contact application</title> </head> <body> <header> <h1 align:'center'>contact app</h1> </header> <main> <div class='container' ng-controller="contactcontroller ctrl"> <div class='row'> <div class='col-md-6'> <ul class="list-group"> <li class="list-group-item" ng-repeat="con in ctrl.contacts" data-ng-click="ctrl.selectuser($index)"> <span>{{con.name.first+ " "+con.name.last}}</span> </li> </ul> </div> <div class='col-md-6'> <div class="media"> <div class="media-left"> <a href="#"> <img data-ng-src="{{ctrl.selecteduser.picture.medium}}"> </a> </div> <div class="media-body"> <h4 class="media-heading">{{ctrl.selecteduser.name.first+ " "+ctrl.selecteduser.name.last}}</h4> <button data-ng-hide="ctrl.editmode" data-ng-click="ctrl.toggleeditmode()"> edit </button> <button data-ng-show="ctrl.editmode" data-ng-click="ctrl.saveuser()"> save </button> <div> <p> <h6>location</h6> </p> <p> <span>street:</span> <span data-ng-hide="ctrl.editmode">{{ctrl.selecteduser.location.street}}</span> <input type="text" data-ng-model="ctrl.selecteduser.location.street" data-ng-show="ctrl.editmode"></input> </p> <p> <span>city:</span> <span data-ng-hide="ctrl.editmode">{{ctrl.selecteduser.location.city}}</span> <input data-ng-show="ctrl.editmode" data-ng-model="ctrl.selecteduser.location.city"></input> </p> <p> <span>state:</span> <span data-ng-hide="ctrl.editmode">{{ctrl.selecteduser.location.state}}</span> <input data-ng-show="ctrl.editmode" data-ng-model="ctrl.selecteduser.location.state"></input> </p> <p> <span>postcode:</span> <span data-ng-hide="ctrl.editmode">{{ctrl.selecteduser.location.postcode}}</span> <input data-ng-show="ctrl.editmode" data-ng-model="ctrl.selecteduser.location.postcode"></input> </p> </div> <div> <p> <h6>phone</h6> </p> <p> <span data-ng-hide="ctrl.editmode"> {{ctrl.selecteduser.phone}}</span> <input type="text" data-ng-model="ctrl.selecteduser.phone" data-ng-show="ctrl.editmode"></input> </p> </div> <div> <p> <h6>email</h6> </p> <p> <span data-ng-hide="ctrl.editmode"> {{ctrl.selecteduser.email}}</span> <input type="text" data-ng-model="ctrl.selecteduser.email" data-ng-show="ctrl.editmode"></input> </p> </div> </div> </div> </div> </div> </main> </body> </html>
my contactdata.service.js file
(function(){ var app=angular.module("mycontactapp"); app.service("contactdatasvc",function($http){ var self=this; self.getcontacts=function(){ var response1=$http.get('http://localhost:3000/contacts'); var response2=response1.then(function(response){ return response.data; }); return response2; } self.saveuser=function(userdata){ $http.put('http://localhost:3000/contacts/'+userdata.id,userdata) .then(function(response){ console.log(response); }) } }); })();
my controller file:
(function(){ var app=angular.module("mycontactapp"); app.controller("contactcontroller",contactctrl); function contactctrl(contactdatasvc){ var self=this; self.editmode=false; contactdatasvc.getcontacts().then(function(data){ self.contacts=data; }) this.selectuser=function(index){ this.selecteduser = this.contacts[index]; } this.toggleeditmode=function(){ this.editmode = !this.editmode; } this.saveuser=function(){ this.toggleeditmode(); var userdata=this.selecteduser; contactdatasvc.saveuser(userdata); } } })();
regards, bhaskar
Comments
Post a Comment