Using ng-click to change the factory value

I have a factory in which I set the language property for the string. Now I want to change this property when the user clicks an image on my site (the idea here is to change the site of the entire page by clicking on the flag). While the language of the pages changes (using ng-showfor this), when I change it manually, but I can not get it to change when I click on the image.

my factory:

'use strict';

angular.module('langService', [])
    .factory('Language', function() {
        var myFactory = {};

        myFactory.language = 'slo';

        return myFactory;
    });

my controller:

'use strict';

angular.module('ZICApp')
    .controller('navbarController', function ($scope, Language) {
        $scope.language = Language.language;
    });

my html:

  <div id="navbar" ng-controller="navbarController">
    <ul>
      <li><a ng-click="language = 'eng'"><img src="images/EN.gif"></a></li>
    </ul>
  </div>

The content that needs to be changed is located inside the view file, as well as in the template. If you need more information about the project, please let me know, try to trim it as much as I can.

+4
2

factory ,

        myFactory.setLanguage = function(lang) {
            myFactory.language = lang;
        };

,

        $scope.changeLanguage = function(lang) {
            Language.setLanguage(lang);
        }

ng-click changeLanguage('eng')


factory:

'use strict';

angular.module('langService', [])
    .factory('Language', function() {
        var myFactory = {};

        myFactory.language = 'slo';
        myFactory.setLanguage = function(lang) {
            myFactory.language = lang;
        };

        return myFactory;
});

:

'use strict';

angular.module('ZICApp')
    .controller('navbarController', function ($scope, Language) {
        $scope.language = Language.language;
        $scope.changeLanguage = function(lang) {
            Language.setLanguage(lang);
        }
    });

HTML:

<div id="navbar" ng-controller="navbarController">
    <ul>
        <li><a ng-click="changeLanguage('eng')"><img src="images/EN.gif"></a></li>
    </ul>
</div>

skubski , . factory :

angular.module('langService', [])
    .factory('Language', function() {
        var language = 'slo';

        function setLanguage(lang) {
            //...optional logic
            language = lang;
        }

        function getLanguage() {
            return language;
        }

        return {
            setLanguage: setLanguage,
            getLanguage: getLanguage
        };
});

$scope.language = Language.language; - $scope.language = Language.getLanguage();

+5

"." dot notation, , .

factory :

var myFactory = {};
var myFactory.settings = {
    currentLanguage : 'slo'
};
return myFactory;

:

$scope.languageProvider = Language;

:

<li><a ng-click="languageProvider.settings.currentLanguage = 'eng'"><img src="images/EN.gif"></a></li>

, , getter/setter .

/getter, iWork.

+1

Source: https://habr.com/ru/post/1616542/


All Articles