1

I'm trying to upgrade an AngularJS template we're using to its latest version.
Now, this template upgraded to AngularJS 1.6.9. But now, after i've upgraded, i'm experiencing a problem that the vm from the parent controller is recognized in the directive's function, only after it exits. This is something that worked before the upgrade and suddenly stoppped working. Here's a sample code for a directive (this problem repeats itself now in all our directives):

(function () {
    'use strict';

    angular
        .module('app.students')
        .directive("studentOverview", StudentOverview);

    function StudentOverviewController(generalSrv, $mdDialog, $q, utilSrv, exportSrv) {

        var vm = this;
        var someField = vm.student.someField;   // vm.student is not recognized now, only after exiting the function... used to work before upgrade
    }

    function StudentOverview() {

        return {
            restrict: "AE",
            templateUrl:
                "app/main/students/views/studentDetails/tabs/studentOverview/studentOverview.html",
            controller: StudentOverviewController,
            controllerAs: "vm",
            scope: {
                student: "="
            },
            bindToController: true
        };
    }
})();  

What am I doing wrong? This code worked perfectly before.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
ashilon
  • 1,791
  • 3
  • 24
  • 41
  • 1
    Initialization logic that relies on bindings being present should be put in the controller's `$onInit()` method, which is guaranteed to always be called *after* the bindings have been assigned. For more information, see [AngularJS Developer Guide - Migrating to V1.6 - $compile](https://docs.angularjs.org/guide/migration#commit-bcd0d4). – georgeawg Mar 14 '18 at 19:04
  • Thanks for the help georgeawg. Sorry about the duplicate though, I didn't find that other post when searching google. – ashilon Mar 16 '18 at 13:57

0 Answers0