0

I'm completely new to AngularJS and I'm converting a small app at the moment, although there's one thing tripping me up.

My goal is to add a module to the dashboard and everything works correctly if I hardcode the JSON data, although, it seems to be having issues when using a variable.

My service:

var pushData = angular.toJson({"modID": "mod_rText_01", "sequence": 1, "group": 1, "dashboard": 1, "type": "text", "content": ["17%", "operating capacity"]});
console.log(' :: Module data pushed: ' + pushData);
$rootScope.$broadcast('locations', pushData); // broadcast to controller

My controller:

// listen for service location updates
$scope.$on('locations', function(event, pushData) {
    console.log(' :: Module data received: ' + pushData);
    $scope.locations.push(pushData);
});

Console logs:

:: Module data pushed: {"modID":"mod_rText_01","sequence":1,"group":1,"dashboard":1,"type":"text","content":["17%","operating capacity"]}
:: Module data received: {"modID":"mod_rText_01","sequence":1,"group":1,"dashboard":1,"type":"text","content":["17%","operating capacity"]}

As mentioned, if I change the controller line to be: $scope.locations.push({"modID":"mod_rText_01","sequence":1,"group":1,"dashboard":1,"type":"text","content":["17%","operating capacity"]});

It works correctly. I feel I'm missing something! Although, according to the console.log, the variable 'pushData' is correct, but nothing happens.

Any help will be greatly appreciated!

0

1 Answer 1

1

The problem is that you transform your object to JSON.

What you are doing here is you actually send a STRING, not an object.

angular.toJson({"modID": "mod_rText_01", "sequence": 1, "group": 1, "dashboard": 1, "type": "text", "content": ["17%", "operating capacity"]});

This string is being published in event and picked up in controller. So in below code, instead of an object, you are really adding a string to an array:

$scope.locations.push(pushData);

Solution: just publish regular JavaScript object instead of string and it should work as expected. $broadcast() and $on() are capable to work with real objects as well.

var dataObj = {"modID": "mod_rText_01", "sequence": 1, "group": 1, "dashboard": 1, "type": "text", "content": ["17%", "operating capacity"]};
$rootScope.$broadcast('locations', dataObj);
Sign up to request clarification or add additional context in comments.

2 Comments

Got it! Thank you very much. Seems I was overcomplicating things. Thanks for the quick reply too, all working. :)
One more thing - I usually use events just for the communication between controllers. If you want to implement regular controller+service scheme, then you would be better off with declaring dependency to the service inside your controller. And make explicit calls from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.