0

I have to assigned a value to a $scope variable inside link function and this value has to be displayed in the UI.But, I am not able to display it on UI.

My code is as follows:

UI:

<div> {{articleContent}}</div>

Link function:

app.directive('member', function($compile,$http,getTocService) {
    return {
        restrict: "A",
        replace: true,
        scope: {
            member: '=',
            articleData: '=',
            articleContent: '=',
            articleDetails: '='
        },
        template: "<div><li ng-if='member.title'><a href='#' ng-click='getContent(member.itemId)'>{{member.title}}</a></li></div>",
    link: function(scope, element, attrs) {
                scope.getContent = function(itemId) {
                    getTocService.getArtData(itemId, function(data){
                        var articleDetails = data.data.getArticleResponse.articleDetail.articleContent;
    scope.$parent.articleContent = articleDetails;

                        alert(articleDetails);
                    });
                }
    }
});

Controller:

app.controller('apdController', function($scope, getTocService,$location) {
    var bookId = $location.search().id;
    var sampdata = getTocService.getToc(bookId);
    $scope.tasks =sampdata;
    $scope.articleContent = '';

});

I want to display articleContent variable's data . Can someone help me please..

6
  • what is link. is it a directive's link? Commented Jan 7, 2016 at 9:19
  • Yes. It is directive. I have updated the code now. Commented Jan 7, 2016 at 9:19
  • is id="articleContent" is a view of directive or your main html? Commented Jan 7, 2016 at 9:24
  • That is main html. I just used it as I was trying to do something with javascript. no need of id there.. Commented Jan 7, 2016 at 9:32
  • so basically you want to display directive's scope variable in your main html. right? Commented Jan 7, 2016 at 9:34

1 Answer 1

1
link: function(scope, element, attrs) {
      scope.getContent = function(itemId) {
      getTocService.getArtData(itemId, function(data){
            var articleDetails = data.data.getArticleResponse.articleDetail.articleContent;

            // Assign your's scope article content here
            scope.articleContent = articleDetails;
            alert(articleDetails);
      });
}

And your html should be look like this,

<member member="myMember" article-data="myData" article-content="articleContent"> </member>
<div> {{articleContent.content}}</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Its not displaying data on UI. no error on browser console too. Do I need to make any change in controller or html?

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.