Amit Jeswani Amit Jeswani - 4 months ago 19
AngularJS Question

Delete Function Not Working In Angular Js

I am not able to get alert for delete function even if i had done right procedure so please help me for the same.Thanks in advance for solving my problem
I am not able to get alert for delete function even if i had done right procedure so please help me for the same.Thanks in advance for solving my problem

<html ng-app="crudApp">
<head>
<meta charset="UTF-8">
<title>Angular js</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
</head>

<body>
<form name="addForm" ng-submit="Add" ng-controller="employeecontroller">
First Name: <input type="text" ng-model="firstname"><br/><br/>
Last Name: <input type="text" ng-model="lastname"><br/><br/>
<input type="submit" name="btnInsert" value="Insert" ng-click='insertData()'/>
</form>

<div ng-controller="DbController">
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr ng-repeat="detail in details">
<td>{{detail.firstname}}</td>
<td>{{detail.lastname}}</td>
<td><input type="button" value="Delete" ng-click="deleteInfo()"/></td>
</tr>
</table>

</div>
<script>

function employeecontroller($scope, $http) {
$scope.insertData = function () {
$http.post("insert.php", {
'firstname': $scope.firstname,
'lastname': $scope.lastname,
}).success(function (data, status, headers, config) {
console.log("Data Inserted Successfully");
});
}
}

var crudApp = angular.module('crudApp', []);
crudApp.controller("DbController", ['$scope', '$http', function ($scope, $http) {
// Sending request to EmpDetails.php files
getInfo();
function getInfo() {
$http.post('select.php').success(function (data) {
// Stored the returned data into scope

$scope.details = data;

console.log(data);
});
}
}]);

function DbController($scope,$http) {

$scope.deleteInfo = function () {
alert("hello");

}
}

</script>

</body>

</html>

Answer

Each controller should be registered as part of your Angular app. You have registered DbController but then created a separate function called DbController that Angular is unaware of. Try moving your delete function to the registered controller. Like this:

 crudApp.controller("DbController", ['$scope', '$http', function ($scope, $http) {
                    // Sending request to EmpDetails.php files
                    getInfo();
                    function getInfo() {
                        $http.post('select.php').success(function (data) {
                            // Stored the returned data into scope

                            $scope.details = data;

                            console.log(data);
                        });
                    }

                   $scope.deleteInfo = function () {
                    alert("hello");
                    }
                 }
Comments