0

I'm trying simple Angular app and I have problem

var module = angular.module("app", []);
module.controller('Main', function ($scope, button) {
    $scope.displayText = button.dispText;

    $scope.increment = function () {
        button.increment();
        $scope.displayText = button.dispText;
    }

})


module.factory('button', function () {
    var displayText = '0';
    var intButton = 0;

    return {
        increment: function () {
            intButton++;
            displayText = intButton.toString()
        },
        getDisplayText: function () {
            return displayText;
        },
        dispText: intButton.toString()
    }
});

Whenever function increment is called variables in my factory get updated, but the value is not transfered to the controller. Why?

1
  • 1
    $scope.displayText = button.dispText; should be $scope.displayText = button.getDisplayText(); Commented Oct 31, 2016 at 16:30

1 Answer 1

0

You need to call the function getDisplayText() from the factory and assign to the variable

    $scope.increment = function () {
     button.increment();
     $scope.displayText = button.getDisplayText();
   }
Sign up to request clarification or add additional context in comments.

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.