How to access widget config from custom javascript

Tags: #<Tag:0x00007efecd5c7bc8> #<Tag:0x00007efecd5c7ad8> #<Tag:0x00007efecd5c7a10>

I’m trying to get my custom javascript to be able to read config values from the settings of a widget. I have the $scope within my controller, but when I print it out to the console I can’t see anything that looks like its config. Here’s the relevant code:

(function(){
  'use strict';

  angular
    .module('RgbLedModule',[])
    .controller('MyController',RgbController);
    RgbController.$inject = ['$scope','OHService'];
    function RgbController($scope,OHService){
      $scope.setTwoColourValues = function(){
        console.log('still angular ftw');
        console.dir($scope);
        ... more code removed
      };
    };
})();


The scope has a bunch of stuff on it, but no config that I can see. it doesn’t look like it is even strictly necessary to inject the scope explicitly as this also works identically:

(function(){
  'use strict';

  angular
    .module('RgbLedModule',[])
    .controller('MyController', function($scope,OHService){
      $scope.setTwoColourValues = function(){
        console.log('still angular ftw');
        console.dir($scope);
        ... more code removed
      };
    };
})();

Within the settings tab of the widget designer I have two defined: “location” and “item_id”, both with type String. This link suggests that once I have $scope I can access the settings using $scope.config.<setting_name> but $scope.config is undefined. So how do I access them from my controller?

I think I’ve found the config by using $scope.$parent.config.<item_id>. I’m not sure why I have to use $parent though. The config is also undefined in the preview pane of the designer, so you have to actually run the widget on a page to see any values, even if defaults are defined.

Very odd, normally you don’t have to use $parent at all.
Not sure why, but here’s a protip™:
add a debugger; statement somewhere in your code and keep the developer’s tools open.
It will trigger a breakpoint, that way you’ll be able to inspect variables in the console and the source file.