Peter Koller Peter Koller - 1 year ago 54
AngularJS Question

Access object passed to a component inside it's own controller

So i got this component. and i can access the data that is passed to its bindings.
But only in it'template. I need to access the object in the component's own controller to do some stuff with it. And i am a bit stuck figuring it out.

Here is the component:

angular.module('MpWatchModule').component('mPointlite', {
bindToController: false,
restrict: 'E',
templateUrl: '/NG-MPWatch/Templates/mPointLite.html',
controller: function (NgMap) {
this.googleMapsUrl = '<my_api_key>';
NgMap.getMap().then(function (map) { = map;'terrain');
this.mpObjs = mpdata;
controllerAs: 'mpl',
bindings: {
mpdata: '<',

And here is the markup in the components template:

<div map-lazy-load="" map-lazy-load-params="{{mpl.googleMapsUrl}}">
<ng-map center="Hungary"
<div ng-repeat="m in mpl.mpObjs">
<marker position="{{m.position}}">

Here is the markup from the page:

<m-pointlite mpdata="mpdl.mpObjs">

And what i will need is to access the objects coming from the mpdl.mpObjs on the page. And do some stuff with them in the components controller, than display it in the components template. I accomplished most of it, just this missing link in the chain.

I appreciate anyone's help, and advise in advance.


Answer Source

Remove bindToController: false

By default an angular component has bindToController set to true and allows you to access the bindings within the scope of the controller.

Then in your controller adjust the line:

this.mpObjs = mpdata;

to be this.mpObjs = this.mpdata;

I would suggest laying the code out like so just for better readability and easier to make changes/work with and follows the angular style guide:

(function () { 'use strict';

    .component('mPointlite', {
        restrict: 'E',
        bindings: {
            mpdata: '<',
        templateUrl: '/NG-MPWatch/Templates/mPointLite.html',
        controller: PointLiteController,
        controllerAs: 'mpl'

PointLiteController.$inject = ['NgMap'];

function PointLiteController(NgMap) {
    var mpl = this;
    mpl.googleMapsUrl = '<my_api_key>';


    function activate() {
        NgMap.getMap().then(function (map) {
   = map;
            mpl.mpObjs = mpl.mpdata;


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download