Marcin Buglewicz Marcin Buglewicz - 4 months ago 12
AngularJS Question

how do you set cookies in angular?

i have angularjs function to translate some text. my entire controller looks like this:

ntsvApp.controller('ntCtrl', function($translate){
var ctrl = this;

ctrl.language = 'en';
ctrl.languages = ['en', 'de'];
ctrl.updateLanguage = function(){
$translate.use(ctrl.language);

};
});


if i put $cookies into updateLanguage function to be able to set cookies, angular doesn't recognise $translate.use method. If I place $cookies inside controller function, it doesn't regosnise $cookies.put method. If i put both cookies and translate in either controller or updatelanguage nothing works. how do i make so writing cookies works only withing updatelanguage function?

Answer

this should work for dependency injection,

ntsvApp.controller('ntCtrl', ['$translate', '$cookies', function($translate, $cookies){
   var ctrl = this;

    ctrl.language = 'en';
    ctrl.languages = ['en', 'de'];
    ctrl.updateLanguage = function(){
        $translate.use(ctrl.language);

    };
}]);

you can use them anywhere in the controller function now.