javascript - I Have Done The AngularJS Code but The Data is not showed up -
please me find problem on coding, have done angularjs code , html , php json encoding cannot fetch data in database... here current code
index.html
<body ng-app="myapp" ng-controller="karyawanctrl"> <form method="post"> <input type="text" ng-model="karyawan.nama"> <input type="text" ng-model="karyawan.alamat"> <input type="submit" ng-click="tambahdata()" value="simpan"> </form> <table ng-init="dapatkandata()"> <thead> <th>nama</th> <th>alamat</th> </thead> <tr ng-repeat="item in datakaryawan"> <td>{{item.nama}}</td> <td>{{item.alamat}}</td> </tr> </table>
here angular code
js/app.js
var app= angular.module("myapp",[]); app.controller("karyawanctrl",function($scope,$http){ //variabel awal $scope.aksi="tambah"; $scope.karyawan={}; //angularjs untuk menyimpan data ke database $scope.tambahdata = function(){ $http.post( 'post.php', { data: $scope.karyawan } ).success(function(data){ alert("data berhasil dimasukkan"); }).error(function(){ alert("gagal menyimpan data"); }); //angularjs untuk menampilkan data ke tabel $scope.dapatkandata = function(){ $http.get('karyawan.php').success(function(data){ $scope.datakaryawan = data; }); }; }; }); here php
$koneksi = mysqli_connect("localhost","root","","belajar") or die("tidak bisa tersambung ke database"); $perintah_sql = "select * karyawan"; $data = []; $result = mysqli_query($koneksi,$perintah_sql); while($row= mysqli_fetch_array($result)){ $temp_data = []; $temp_data['nama']= $row['nama']; $temp_data['alamat']=$row['alamat']; array_push($data,$temp_data); } echo json_encode($data);
the problem dapatkandata function defined inside of tambahdata function. move outside , should work:
var app= angular.module("myapp",[]); app.controller("karyawanctrl",function($scope,$http){ //variabel awal $scope.aksi="tambah"; $scope.karyawan={}; $scope.tambahdata = function(){ $http.post('post.php',{ data: $scope.karyawan }).success(function(data){ alert("data berhasil dimasukkan"); }).error(function(){ alert("gagal menyimpan data"); }); }; $scope.dapatkandata = function(){ $http.get('karyawan.php').success(function(data){ $scope.datakaryawan = data; }); }; $scope.dapatkandata(); // use instead of ng-init. remove html });
Comments
Post a Comment