简体   繁体   中英

Issue with $http service in Angular 1.2

I just started using Angular 1.2.0 in my development app, and I noticed that the following function doesn't work anymore, take a look:

var myItems = angular.model('myItems', []);

myItems.controller('itemsController', function($scope, $http) {

    // delete item from the database
    $scope.deleteItem = function(id) {
        $http.delete('/api/items/' + id)
            .success(function(data) {
                $scope.items = data;
            })
            .error(function(data) {
                // log error 
            });
    };
});

Then in my view, this is what triggers deleting an item:

<input type="checkbox" data-ng-click="deleteItem(item._id)"> {{ item.text }}

I'm very fresh to Angular, so I'm not sure what's exactly going wrong here, and a look at the changelog file for version 1.2 on the Angular repository didn't yield an answer. Can somebody with more experience in Angular please explain to me what exactly the problem is here?

Edit : here's the log from the Chrome error console, which is visible as soon as the page is loaded. Clicking the checkbox to delete an item does nothing.

Error: [$parse:isecprv] http://errors.angularjs.org/undefined/$parse/isecprv?p0=deleteItem(item._id)
at Error (<anonymous>)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:6:453
at ha (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:84:103)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:87:372
at Array.forEach (native)
at q (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:7:261)
at rc (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:87:354)
at Jb.readIdent (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:149:31)
at Jb.lex (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:144:199)
at Ya.parse (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:151:12) <input type="checkbox" data-ng-click="deleteItem(item._id)">

Update : It turns out that one of the breaking changes in Angular 1.2.0-rc2 (currently latest stable build) is the introduction of private properties on the scope chain. This potentially breaks a lot of apps that stores data in document-oriented databases such as, in my case MongoDB . If you're someone facing this same issue, you can either go back to version 1.2.0-rc3 (Google CDN here ) for now or wrap your sensitive APIs in a closure/controller as suggested in the changelog.

In error console first link points to the page

http://docs.angularjs.org/error/$parse:isecprv?p0=deleteItem(item._id)

This page explains that the error is that you use private property in expression.

The expression is deleteItem(item._id) .

_id - is a private property of item.

Try changing the field name ( _id ) . This error is clear from the error console.

From Angular version 1.2

Referencing private fields in Angular expressions is disallowed! Expression: deleteItem(item._id)

Fields with names that begin or end with an underscore are considered private fields. Angular expressions are not allowed to reference such fields on the scope chain.

More info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM