2

Receiving error cannot read property 'type' of undefined at PollListCtrl.runQuery. I'm not sure why this is coming up undefined. I've console logged profile-polls.controller.js where listConfig is created to make sure there is an object here. The response is

{type: "all", filters: {pollsCreated: Array(3)}}

But it is coming up undefined when I try to pass it to poll-list component in the profile-polls.html. Below is the gist for the relevant files. Can anyone tell me why this is not passing correctly?

https://gist.github.com/RawleJuglal/fa668a60e88b6f7a95b456858cf20597

2
  • If you put a breakpoint on line 57, is queryConfig undefined? Commented Aug 22, 2017 at 17:59
  • My debug skills aren't strong so I'm not sure how to breakpoint looking at it in chrome, but I did a console log of queryConfig before this._Polls.query but it does not get that far. I assume because it is throwing the error at line 41. Commented Aug 22, 2017 at 18:16

2 Answers 2

1

I think you need to define watcher for your component. On start listConfig is undefined and only after some delay (next digest cycle) it gets value. So we create watcher and cancel it after if statement.

app.component('pollList', {
  bindings: {
    listConfig: '='
  },
  templateUrl: 'poll-list.html',
  controllerAs: 'vm',
  controller: PollListCtrl
});

function PollListCtrl($scope) {

    var vm = this;

    function run(){
      console.log(vm.listConfig);
    }


    var cancelWatch = $scope.$watch(function () {
        return vm.listConfig;
    },
    function (newVal, oldVal) {

        if (newVal !== undefined){
            run();
            cancelWatch();
        }
    });
}

Demo plunkr

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry tried this but no change. It was also my understanding that controllerAs in the config would do the same thing. Is that not the case?
0
  • You should never use $scope in your angularjs application.
  • You can use ngOnInit() life cycle hook to access "bindings" value instead constructor.

    ngOnInit(){

    $ctrl.listConfig = { type: 'all' };

    $ctrl.limit = 5;

    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.