Solved

Passing variable to function in Angular JS

Posted on 2014-10-15
2
204 Views
Last Modified: 2014-11-06
I want to call a function in the controller file that should in turn call the API and delete a post

If I replace the bold bit in the code '{{user.id}}' with a static number, it works fine - when I try different ways of including it - any of these below, I get errors, most of the time it just passed the code as text

ng-click="deleteUser('{{user.id}}')"

Gives me a source as =
<button class="btn btn-small btn-purple" ng-click="deleteUser('580')" style="margin:0 0 2px 0; font-weight:bold;">DELETE</button>

And that should work as this static code
ng-click="deleteUser('580')"

Works fine


HTML:
<button class="btn btn-small btn-purple" ng-click="deleteUser('{{user.id}}')" style="margin:0 0 2px 0; font-weight:bold;">DELETE</button>

controller.js:
$scope.deleteUser = function(userId) {
			$http({
		    method: 'DELETE',
		    url: '/user/delete/' + userId,
		    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
		    transformRequest: function(obj) {
		        var str = [];
		        for(var p in obj)
		        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
		        return str.join("&");
		    }
		})
		.success(function () {
			refreshUserList();
		})
		
		.error(function() {
		});

Open in new window




What can be wrong, for some reason when I'm entering the dynamic variable, even though the source is fine, identical to the static working source, when debugging I'm receiving this

curl 'http://URL:8080/user/delete/%7B%7Buser.id%7D%7D' -X DELETE -H 'Host: URL:8080' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0' -H 'Accept: application/json, text/plain, */*' -H 'Accept-Language: en-US,en;q=0.5' -H 'Accept-Encoding: gzip, deflate' -H 'Referer: http://URL:8080/admin/viewCustomers.php' -H 'Cookie: JSESSIONID=8144C37C60F584EBBEB9EC5131F0301D'

curl 'http://URL:8080/user/delete/%7B%7Buser.id%7D%7D' - as you can see it distorts it - not when I'm sending static, but only when the dynamic is passed - even though the source says the same... very confused.
0
Comment
Question by:DanteAdmin
  • 2
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40381856
simply use : ng-click="deleteUser(user.id)"
Test page : http://jsfiddle.net/6d0y9d3h/
https://docs.angularjs.org/api/ng/directive/ngClick
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40426331
<div ng-app="myApp" ng-controller="myCtrl">
    <button class="btn btn-small btn-purple" ng-click="deleteUser(user.id)" style="margin:0 0 2px 0; font-weight:bold;">DELETE</button>
</div>

Open in new window


var myApp = angular.module("myApp", []);
var myCtrl = myApp.controller("myCtrl", function($scope,$http) {
    $scope.user = {
        id: 580
    };

    $scope.deleteUser = function(userId) {
        alert(userId);
			$http({
		    method: 'DELETE',
		    url: '/user/delete/' + userId,
		    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
		    transformRequest: function(obj) {
		        var str = [];
		        for(var p in obj)
		        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
		        return str.join("&");
		    }
		})
		.success(function () {
			refreshUserList();
		})
		
		.error(function() {
		});
    };
});

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

3 Experts available now in Live!

Get 1:1 Help Now