javascriptangularjsangularjs-scopeangularjs-controlleras

controller as syntax not working with angular 1.4.5 var vm = this template not showing


I had switched to controller as syntax with angular 1.5 -1.6 projects etc... The particular project I am need to add some functionality to a angular 1.4.5 project.

Usually I using angular routing etc.., but this project is setup differently.

problem is in my template html, i'm able to see

$scope.blah   //  {{ blah}} 

but i am not understanding why or how to access this

controller:

icisNg.controller("hearingListing", ['$scope', '$rootScope',
    function ($scope, $rootScope) {

        $scope.blah = 333;
        var vm = this;
        vm.whatever = 'controller as';
        $scope.atEnd = "endsdfgsdfgsdfg";
        console.log('scope', $scope.atEnd);
        console.log('vm', vm.whatever);

    }

]);

Html template:

<div fieldset-wrapper legend="Hearing" ng-controller="hearingListing">

    <div>test again</div>

    <div>{{blah}}</div>

    eewr
    <br />
    vm:{{vm.whatever}}
    <br />
    {{atEnd}}
</div>

Solution

  • The most expedient, based upon how your code is presented is to...

    Change

    ng-controller="hearingListing"
    

    To

    ng-controller="hearingListing as vm"
    

    ===

    Also, please keep in mind that the variable vm in the controller does not refer to the same vm in your template. They are different and can be named anything.