Policy receives no value

0

I have this directive:

        app.directive('mensagem', function($timeout, $rootScope){
        return {
            restrict: 'E',
            scope: {
                msg: '='
            },
            template: '<div ng-show="animate && msg" class="alert alert-success animate-show animate-hide" ng-bind="msg"></div>',
            link: function(scope, elem, attrs){
                scope.animate = true;
                $timeout(function(){
                    scope.animate = false;
                    scope.msg = undefined;              
                },1500);
            }
        }
    })

I have a controller called 'ArticlesController' with a 'saveArticle' method.

            $scope.salvaArtigo = function(){
            $http.post(url+'artigo', $scope.artigo).then(function(data){
                if(data.data.sucesso){
                    $rootScope.mensagem = data.data.mensagem;
                    $scope.artigo = {};
                    $state.go('dash');
                }
            })
        }

I also have another controller called 'ListArtsControler', with a method to enable or disable an article.

            $scope.ativarArtigo = function(artigo){
            $http.post(url+'remover-artigo/'+artigo.id).then(function(data){
                if(data.data.sucesso){
                    $rootScope.mensagem = data.data.mensagem;
                    $scope.listaArtigos();
                }
            });
        }

And finally, my html with the directive looks like this:

<mensagem msg="mensagem"></mensagem>

When I save the article, the server message appears normally in the policy, but when I activate / deactivate, no message appears. I put it in the {{message}} html and it appears normal, but the value is not passing to the directive. If I put something like $ rootScope.message = 'test' out of the enable / disable method, then yes it passes the value.

    
asked by anonymous 03.03.2017 / 12:30

1 answer

0

I solved the problem. The solution was to put the $ watch inside the directive, thus:

        app.directive('mensagem', function($timeout, $rootScope, $compile){
        return {
            restrict: 'E',
            transclude: true,
            scope: {
                msg: '='
            },
            template: '<div ng-show="animate && msg" class="alert alert-success animate-show animate-hide" ng-bind="msg"></div>',
            link: function(scope, elem, attrs){
                scope.animate = true;
                $timeout(function(){
                    scope.$watch('msg', function(newVal, oldVal){
                        console.log(scope.msg);
                    }, true);

                },1500);
            }
        }
    })
    
03.03.2017 / 20:48