4

Structure

statsController.js

(function (module) {
    'use strict';

    var statsController = function ($scope, $rootScope, $timeout, $routeParams, $location, $window, statsService) {
        $scope.$on('$viewContentLoaded', function (event) {
            $window.ga('send', 'pageview', { page: $location.url() });
        });

        var model = {};
        model.stats = {};

        statsService.getStats().then(function (d) {
            model.stats = d;
        });
    };

    module.controller("statsController", statsController);

}(angular.module("app")));

statsService.js

(function (app) {

    var statsService = function (webapi) {

        var model = {};

        model.getStats = function () {
            return webapi.get('stats/getstats');
        }

        return model;
    };

    app.factory("statsService", statsService);
    statsService.$inject = ['webapi'];

}(angular.module("app")))

stats.html

Total paid customers: {{statsController.model.totalPaidCustomers}}<br/>
Stripe confirmed customers: {{statsController.model.stripeConfirmedCustomers}}<br />

Result from API:

{"totalPaidCustomers":1,"stripeConfirmedCustomers":2}

When I put alert() in statsController.js for d.totalPaidCustomers I get value 1, same for other parameter.

So only problem is to show this in html.

App.js

.when('/stats', {
            templateUrl: 'tpl/admin/stats.html',
            controller: 'statsController',
            controllerAs: 'stats'
          }).
2
  • your model isn't on $scope in the statsController. Commented Feb 19, 2016 at 13:15
  • Are you using ui-router? Commented Feb 19, 2016 at 13:30

3 Answers 3

1

If i understand correctly. It should be "model.stats"

Total paid customers: {{statsController.model.totalPaidCustomers}}

http://jsfiddle.net/f5hb9spz/8/

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

Comments

0

Try changing your state definition to this:

.when('/stats', {
    templateUrl: 'tpl/admin/stats.html',
    controller: 'statsController',
    controllerAs: 'vm'
}).

Then change the controller to this:

(function (module) {
    'use strict';

    var statsController = function ($scope, $rootScope, $timeout, $routeParams, $location, $window, statsService) {
        $scope.$on('$viewContentLoaded', function (event) {
            $window.ga('send', 'pageview', { page: $location.url() });
        });
        var vm = this;
        vm.model = {};
        vm.model.stats = {};

        statsService.getStats().then(function (d) {
            vm.model.stats = d;
        });
    };

    module.controller("statsController", statsController);

}(angular.module("app")));

Finally your view goes to this:

Total paid customers: {{vm.model.stats.totalPaidCustomers}}<br/>
Stripe confirmed customers: {{vm.model.stats.stripeConfirmedCustomers}}<br />

If you don't see anything then just put this in the view and see what you have:

{{ vm | json }}

5 Comments

Nothing shows in html, I've updated my question with App.js routing settings. This is from console.log for model.stats Object { totalPaidCustomers=1, stripeConfirmedCustomers=2}
You put ALL of those bindings in and see nothing? I updated the answer to include a few more things.
I have this in console.log Object { totalPaidCustomers=1, stripeConfirmedCustomers=2} for model.stats and this is html inspect: puu.sh/ndCHc/cad2ecfb4f.png
for some reason it doesn't show anything in html. it's breaking my brain
Can you reproduce in a fiddle or plunker? Do you see any other random errors in your browser's dev tools console log?
0

Problem was I didn't assign the current scope to a model.

var model = this;

statsController.js

(function (module) {
    'use strict';

    var statsController = function ($scope, $rootScope, $timeout, $routeParams, $location, $window, statsService) {
        $scope.$on('$viewContentLoaded', function (event) {
            $window.ga('send', 'pageview', { page: $location.url() });
        });

        var model = this;
        model.stats = {};

        statsService.getStats().then(function (d) {
            model.stats = d;
        });
    };

    module.controller("statsController", statsController);

}(angular.module("app")));

Then call in html:

Total paid customers: {{stats.stats.totalPaidCustomers}}<br/>
Stripe confirmed customers: {{stats.stats.stripeConfirmedCustomers}}<br />

have to change name of var though(stats.stats), confused the hell out of me.

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.