22

I am trying to use AngularJS in my application and have been successful to some extent.

I am able to fetch data and display it to the user. And I have a button in ng-repeat via which I want to post DELETE request. Below is my code which does it.

<div class="navbar-collapse collapse">
    <table class="table table-striped" ng-controller="FetchViewData">
        <tr>
            <td>Name</td>
            <td>ID</td>
            <td>Department</td>
            <td></td>
        </tr>
        <tr ng-repeat="d in viewData">
            <td>{{d.EmployeeName}}</td>
            <td>{{d.EmployeeID}}</td>
            <td>{{d.EmployeeDepartment}}</td>
            <td>
                <button class="trashButton" type="button" 
                name="view:_id1:_id2:_id14:_id24:btnDelete" 
                id="view:_id1:_id2:_id14:_id24:btnDelete" 
                ng-click="deleteRecord('{{d['@link'].href}}')">
                <img src="/trashicon.gif"></button>
            </td>
        </tr>
    </table>
</div>

This is the FetchViewData function which fetches the information and displays it to the user.

function FetchViewData($scope, $http) {
    var test_link = "<MY LINK>";
    $http.get(test_link).success( function(data) {
        $scope.viewData = data;
    });
}

The data is fetched and properly displayed.

But the code in ng-click="deleteRecord('{{d['@link'].href}}')" does not fire when delete button is clicked. In Google Chrome's developer tools I can see valid values are generated for code {{d['@link'].href}} but the code deleteRecord does not get fired. From this question I tried removing the braces and writing only d['@link'].href but it didn't work for me.

When I replace ng-click with onclick the deleteRecord function gets fired.

function deleteRecord(docURL) {
    console.log(docURL);

    $http.delete(docURL);
}

But then I receive the below error.

Uncaught ReferenceError: $http is not defined
deleteRecord
onclick

I am using jQuery 1.10.2 and AngularJS v1.0.8.

Community
  • 1
  • 1
Naveen
  • 6,786
  • 10
  • 37
  • 85

7 Answers7

26

FetchViewData here is a controller, and in your html, where you have ng-controller="FetchViewData", you are telling it to look within that controller's scope for any angular methods and variables.

That means, if you want to call a method on click, it needs to be calling something attached to your controller's scope.

function FetchViewData($scope, $http) {
    var test_link = "<MY LINK>";
    $http.get(test_link).success( function(data) {
        $scope.viewData = data;
    });
    $scope.deleteRecord = function(docURL) {
        console.log(docURL);

        $http.delete(docURL);
    }   
}

Here, the function exists on the scope, and any html that is inside your FetchViewData Controller has access to that scope, and you should be able to call your methods.

It's working when you use on-click because your function exists in the global namespace, which is where on-click is going to look. Angular is very heavily reliant on scoping to keep your namespaces clean, there's lots of info here: https://github.com/angular/angular.js/wiki/Understanding-Scopes

pm.calabrese
  • 386
  • 6
  • 10
B Cotter
  • 951
  • 5
  • 7
  • I tried using the code `ng-click="FetchViewData.deleteRecord('{{d['@link'].href}}')"` and `ng-click="deleteRecord('{{d['@link'].href}}')"`, but in both cases it didn't work. – Naveen Oct 22 '13 at 08:52
  • 2
    There will be a problem in your syntax somewhere then - as you can see, this very basic fiddle works - http://jsfiddle.net/HB7LU/799/ Try replacing your method with one without the argument binding (that might be what's throwing your error), and see if it's calling it, so just ng-click="deleteRecord()". – B Cotter Oct 22 '13 at 09:07
  • 2
    Thanks! It finally worked using `ng-click="deleteRecord(d['@link'].href)"`. Thanks again for the GitHub link and JSFiddle. – Naveen Oct 22 '13 at 09:20
  • Great job explaining this, helped me out a lot! – Ryan May 07 '15 at 18:54
6

INSTEAD of this

ng-click="deleteRecord('{{d['@link'].href}}')"

TRY this

ng-click="deleteRecord(d['@link'].href)"

You don't need to use curly brackets ({{}}) in the ng-click

ENJOY...

Rajsekhar Das
  • 61
  • 1
  • 3
4
  function deleteRecord(docURL) {
       console.log(docURL);

       $http.delete(docURL);
 }

It should be

 $scope.deleteRecord = function (docURL) {
         console.log(docURL);

         $http.delete(docURL);
}

EDIT: change something in html and controller ....

SEE WORKING DEMO

Nitish Kumar
  • 4,850
  • 3
  • 20
  • 38
3

The deleteRecord method should be assigned in the current and correct scope

$scope.deleteRecord = function(){
....
subZero
  • 5,056
  • 6
  • 31
  • 51
1

Another possibility for why ng-click does not fire, is that you are apply a CSS style of pointer-events:none; to the element. I discovered that Bootstrap's form-control-feedback class applies that style. So, even though it raises the z-index by 2 so that the element is in front for clicking, it disables mouse-clicks!

So be careful how your frameworks interact.

Zarepheth
  • 2,465
  • 2
  • 32
  • 49
0

As mentioned, the function should be created inside the scope:

 $scope.deleteRecord = function (docURL) {
         console.log(docURL);

         $http.delete(docURL);
}

To use the function, first drop the "{{ }}" since you are using it from inside an ng-repeat. Another issue is the use of apostrophe in your code, you have two pairs one inside the other... well I am sure you get the problem with that.

Use the function like so:

ng-click="deleteRecord(d['@link'].href)"

Best of luck !

0

If you want to use as a submit button the set the type to 'submit' as:

<button type="submit" ...
showdev
  • 28,454
  • 37
  • 55
  • 73
frank
  • 153
  • 1
  • 2
  • 1
    It looks like you intended to say something more - this answer isn't complete. – Mogsdad Apr 01 '15 at 14:23
  • 1
    No idea what I was thinking at the time. ng-click="deleteRecord(d['@link'].href)"Was the right answer – frank Apr 13 '16 at 09:23