从链接函数angularJs在UI中显示数据

时间:2016-01-07 09:15:00

标签: javascript angularjs

我必须在链接函数内为$ scope变量赋值,并且该值必须显示在UI中。但是,我无法在UI上显示它。

我的代码如下:

UI:

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

链接功能:

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);
                    });
                }
    }
});

控制器:

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

});

我想显示articleContent变量的数据。请有人帮帮我..

1 个答案:

答案 0 :(得分: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);
      });
}

你的HTML应该是这样的,

<member member="myMember" article-data="myData" article-content="articleContent"> </member>
<div> {{articleContent.content}}</div>