0

I have the following Angular controller with a simple http get in it:

angular.module('SCtrl', ['ng-fusioncharts']).controller('SCtrl', function($scope, $http) {

    //Get All errors.
    $http({
        method: 'GET',
        url: 'node/errors'
    }).then(function successCallback(response) {
        $scope.notifications = response.data;
        console.log($scope.notifications);
    }, function errorCallback(response) {

    });
}

This works fine.

I then have the following directive. I want to be able to put {{notifications}} in the module body, however it doesn't work. I am guessing this is because I give the directive its own scope. What is the correct way to link scopes? My attempt is below but this doesn't work.

Thanks!

angular.module('SCtrl').directive('listDirective',function(){
    return{
        restrict:'E',
        scope:{
            listTitle:"@",
            notifications:"="
        },
        templateUrl: '../templates/listWidget.html',
        link: function(scope, element, attrs){
            $scope.notifications=scope.notifications;
        }
    }
});
1
  • If you use the notifications directly in your listWidget.html, you can just do <list-directive notifications="notifications"></list-directive> Commented Jan 17, 2017 at 14:20

2 Answers 2

2

Mis uderstood initially, as @devqon pointed out, you need to pass your notification object in list-directive which can be accessed in your listWidget.html

<list-directive notifications="notifications"></list-directive>
Sign up to request clarification or add additional context in comments.

4 Comments

Better :) But don't know if complete, because it seems OP wants to have the notifications in the link function for whatever (unmentioned) reason
@devqon: not needed actually, that's where initially i got confused... as the object should be available in the view... unless he wants to do some sort of manipulation again
Thanks for this... this works but is there a way to do it I don't need to pass it in the html? What's best practice?
@MattBoyle: typically you would pass the value, if you don't want then you need to remove the scope object completely which would not create an isolate scope.. and access your controller scope directly
0
<list-directive notifications="notifications"></list-directive>

link: function(scope, element, attrs){
            scope.notifications=scope.notifications;
        }

You should assign the values notifications="notifications" otherwise you can't get. Or else you can trigger API call in directive itself. Now you don't need to assign notifications="notifications".

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.