2

I have one problem. I'm using Angular localStoragee module to store settings, which i set in controller with:

localStorageService.set('localStorageKey','Add this!');

It's working very well but how can i watch localStorageKey for changes in directive, which takes it's value as a parameter?

I tried something like:

.directive('fooBar', function(localStorageService){
    var linker = function(scope, element, attrs){

        var unit = localStorageService.get('localStorageKey');
        $watch('unit', function(){
            console.log(unit);
        });

    }

    return {
        restrict: 'AE',
        scope: {
            value: '@dataValue'
        },
        replace: true,
        link: linker
    }
});

But no luck, i don't think i can watch variables like this. How can i solve this problem?

Thanks.

1 Answer 1

4

This should work:

$scope.$watch(function() {
    return localStorageService.get('localStorageKey');
}, function(value) {
    console.log(value);
});
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.