Angularjs and spark

Hi
I am trying to make a basic UI for spark using angularjs.
I tried to use this example from this topic : http://stackoverflow.com/questions/28522449/consuming-spark-rest-api-with-angularjs.

to get a variable but with no success.
Did anyone tried anything similar, or have some experience with angular and can check it?

Here is my code on jsfiddle : https://jsfiddle.net/clat/kpdx4dcs/1/

I havent done it using Resources… but I have done it via $http.

So in your Controller declare it like this:

var MainCtrl = function ($scope, $location, $route, $http) {
     //Your code here
}

Here is a POST example:

$http({
    method: 'POST',
    url: 'https://api.spark.io/v1/devices/' + SparkDeviceID + '/Trigger?access_token=' + SparkAccessToken,
    data: $.param({args: argsData}),
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data, status, headers, config) {    	 
})
.error(function(data, status, headers, config) {
    alert('Error' + data);
});

Here is a GET:

$http({
    method: 'GET',
    url: 'https://api.spark.io/v1/devices/' + SparkDeviceID + '/DoorStatus' + '?access_token=' + SparkAccessToken,
})
.success(function(data, status, headers, config) {	 
    if (data.result == 0) 
	$scope.State = 'Closed';
    else
	$scope.State = 'Open';			
})
.error(function(data, status, headers, config) {
    alert('error ' + data);
    debugger;	    	
});

There is no reason why a Resource wont work, but I’ll have to look later tonight and see why. I figured I just post some $http code that works.

Enjoy.

2 Likes

Thanks for your reply Carsten4207.
I’ll check it.

If you look at the console you’ll notice it’s complaining about the ngResource. Turns out you need to load that externally. If you add this link (https://code.angularjs.org/1.0.7/angular-resource.min.js) to the external resources, you should at least be able to retrieve a variable. Then you’ve got to figure out the dependencies of $interval.

Is there a specific need that requires you to use variables? Instead of polling for data, you could have the Core publish data every second.

1 Like

I didn’t know there was a console to jsfiddle… Thank you.
I thought it would be easier to retrieve variable, I would check if listening to an event is easier.

1 Like