0

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 :/

2 Answers 2

1

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

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

12 Comments

if i write this directly vm isn't defined anymore ?
@Orsu Yes within ProfController vm isn't defined anymore
ohh Sorry this should be $scope.vm.documents = Data.documents; i forget $scope with vm
Remove ng-controller from your View
|
0

In the end i found myself - I put the controller back like it was, i renamed all the {{card.something}} as {{document.something}} and i removed the ng-bind="" in the <div> that was being looped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.