如何将变量从html传递给指令AngularJS?

时间:2016-05-19 07:30:07

标签: javascript jquery html angularjs

我有一个HTML,我需要显示一个弹出窗口。我有这个:

<div class="dashboardTr" ng-repeat="post in posts">
  <modal-dashboard header="Success" body="post.ID" id="success"></modal-dashboard>
</div>

和这个指令:

app.directive('modalDashboard', function() {
    return {
        restrict: 'E',
        scope: {
            header: '@header',
            body: '=',
            id: '@id'
        },
        templateUrl: '/modalDashboard.html'
    }
});

弹出窗口的新HTML是:

<div class="modal-content">
  <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title">{{ header }}</h4>
  </div>
  <div class="modal-body">
              <p>{{ body }}</p>
  </div>
</div>

它不起作用。如果我将一个字符串传递给body属性,它就可以了。有什么问题? 它在另一个指令中。可能是问题吗? 谢谢!

1 个答案:

答案 0 :(得分:-1)

这应该有效:

app.directive('modalDashboard', function() {
return {
    restrict: 'E',
    scope: {
        header: '@header',
        body: '=body', // <-- make sure to modify this to evaluate body attribute
        id: '@id'
    },
    templateUrl: '/modalDashboard.html'
}
});