1

Fairly new to ANGULAR!!

I have created a "directive" named "contactCard". What I'm trying to achieve with the help of this directive to display some json Data. But unfortunately data is not getting displayed.

[WORKING CODES ARE PLACED HERE ON PLUNKER]

I have this html:

<ul class="digi-alert-list" ng-controller="jsonNotify">
    <li ng-repeat="notifications in notify">
        <contact-card data="notifications"></contact-card>
    </li>
</ul>

And MainAPP file:

(function () {
  var app = angular.module("MainApp", 
  ["ui.bootstrap","app.directives.contactCard"]
);
app.controller('jsonNotify', function($scope, $http) {
  $http.get('notification.json')
       .then(function(res){
          $scope.notify = res.data;                
        });
});
})();

Finally contactCard.js

angular.module('app.directives.contactCard', [])
    .directive('contactCard', function(){
        return{
            restrict: 'E',
            scope: {
                data: '='
            },

            template: '<div class="alert-report"><p>{{notifications.serveactivity}}</p></div>',
            controller: function($scope){
                alert("directives");
            }
        };
    });
2
  • 1
    shouldn't it be data.serveractivity in the directive template,. Commented Feb 6, 2015 at 10:04
  • youtube.com/watch?v=8ILQOFAgaXE Commented Feb 6, 2015 at 19:22

2 Answers 2

2

I think your issue is that you are binding "data" in your scope to notifications, but you are using "notifications" in your directive template.

Your directive template should be:

template: '<div class="alert-report"><p>{{data.serveactivity}}</p></div>',
Sign up to request clarification or add additional context in comments.

Comments

2

If you declare that data is the variable on your scope, like you did here:

scope: {
    data: '='
},

Then in your template, you have to use data, and not notifications:

template: '<div class="alert-report"><p>{{data.serveactivity}}</p></div>',

When you declare your scope like this, it means that it's an "Isolated Scope". It knows only the variables you declared for it. It has no knowledge of the outside world.

You can read more about this here.

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.