Search code examples
javascriptangularjsangularjs-1.6

Angular not binding data on html directly


I'm a beginner in Angular (ver 1.6.3) and i ran into this problem:

i have a controller called prof:

(function () {
    'use strict';

    angular
        .module('app.prof', [])
        .controller('ProfController', ProfController);

    /** @ngInject */
    function ProfController($scope, Data)
    {
        var vm = this;
        vm.documents = Data.documents;
        vm.classes = Data.classes;
    }
})();

Here's its associated module :

(function () {
    'use strict';

    angular
        .module('app.prof')
        .config(config);

    /** @ngInject */
    function config($stateProvider, $translatePartialLoaderProvider, msApiProvider) {
        // State
        $stateProvider
            .state('app.prof', {
                url    : '/prof',
                views  : {
                    'content@app': {
                        templateUrl: 'app/main/prof/prof.html',
                        controller : 'ProfController as vm'
                    }
                },
                resolve : {
                    Data : function(msApi){
                        return msApi.resolve('data@get');
                    }
                }
            });

        $translatePartialLoaderProvider.addPart('app/main/prof');
        msApiProvider.register('data', ['app/data/prof/prof-data.json']);
    }
})();

And here's the main problem : i have this html :

<div class="document" ng-repeat="document in vm.documents">
    <ms-card template="'app/custom-directives/prof-card/prof-card.html'"
             ng-model="document"></ms-card>
</div>

it works perfectly fine, the data is correctly binded and all, but when i put the template called directly in the page instead of calling it throught a <ms-card> it doesn't work anymore ! i've tried to put some console.log() a little eveywhere but it always says the data isn't defined; i don't get it. Plus, the ng-repeat always works fine

Edit : a bit of the html i call :

<md-list-item class="document-item md-white-bg md-2-line" md-ink-ripple>
              <div class="media ml-16">
                <img class="image-apercu" ng-src="{{card.media.image.src}}" alt="{{card.media.image.alt}}" ng-show="card.media.image">
              </div>

ps : when i add the html directly i don't forget to put the ng-model="document" but it still doesn't work

This is very confusing for me :/


Solution

  • You cannot bind the "vm" object with "$scope". you need to bind it with Scope like

    function ProfController($scope, Data)
        {
            //$scope.vm = this;
    
            $scope.vm.documents = Data.documents;
            $scope.vm.classes = Data.classes;
        }
    

    Your Html Should be like :

    <body ng-app="app.prof" ng-controller="ProfController">
        <div class="document" ng-repeat="document in vm.documents">
            <ms-card template="'app/custom-directives/prof-card/prof-card.html'"
                     ng-bind="document"></ms-card>
        </div>
    </body>
    

    Hope this will work for you