需要帮助将图表js添加到模态窗口

时间:2015-05-30 20:41:08

标签: javascript angularjs twitter-bootstrap charts

这是图表

的图表
<div class="diagram">
<canvas id="pie" class="chart chart-pie" width="150" height="150" data="ModalObj.FunctionalTest" labels="labels" colours="Colours">
</canvas>

这是指令

.directive('modal', function () {
    return {
        template: '<ng-include src="getTemplateUrl()"/>',
        restrict: 'E',
        controller: function($scope) {
            //function used on the ng-include to resolve the template
            $scope.getTemplateUrl = function() {
                return $scope.path;
            }
        }
    }});

此外,如果模板网址需要,我必须在模态中添加图表,并非所有模板都使用图表

1 个答案:

答案 0 :(得分:1)

DEMO:http://jsfiddle.net/softvar/9cf47fe1/1/

<强> HTML

<div ng-app="myApp">
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
      Launch demo modal
    </button>

    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
          </div>
          <div class="modal-body">
              <elem-modal></elem-modal>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
</div>

<强> JS

var app = angular.module('myApp', []);
app.directive('elemModal', function () {
    return {
        template: '<p>Test</p><ng-include src="getTemplateUrl()"/>',
        restrict: 'E',
        controller: function($scope) {
            //function used on the ng-include to resolve the template
            $scope.getTemplateUrl = function() {
                return $scope.path;
            }
        },
        link: function (scope) {
            console.log(scope)
        }
    }});