javascript - 使用 AngularJS 在 2 个不同的指令中调用相同的 $http.get()

标签 javascript angularjs d3.js

我正在使用 AngularJS 和 D3.JS

我有一个 html 表单,其中包含以下内容:

    <div simple-chart chart-data="lineData"></div>

这与这样的指令 Hook :

mist.directive("simpleChart", function($window, $http){
    return{
        restrict: "EA",
        template: "<svg width='750' height='200'></svg>",
        link: function(scope, elem, attrs){
            function drawLineChart() {
                //initilize the line chart
            }
            $http.get("myurl")
            .success(function(data, status, headers, config){
                drawLineChart();
            })
        }}
  });
  1. 是否可以使用 $http.get("myurl") 中的数据创建另一个指令,而无需再次调用它?
  2. 是否可以使 $http.get("myurl") 变得通用,以便可以由不同的指令调用?
  3. 我可以使用这样的东西吗? Can't get correct return value from an jQuery Ajax call

最佳答案

您可以将 http 调用包装到服务中并使用类似 angular-cache 的内容。缓存来自服务器的响应。

关于javascript - 使用 AngularJS 在 2 个不同的指令中调用相同的 $http.get(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30772380/

相关文章:

javascript - 如何绘制三 Angular 形符号?

javascript - 如何获取mysql数据库中的账户

Javascript 查找字符串附近的数字

javascript - JS如何继续事件防止默认

angularjs - 我如何使用 Angular js在另一个html文件中使用从json响应获取的数据

javascript - 你能在转换后链接一个函数而不是它成为转换的一部分吗?

javascript - 在哪里可以找到免费的内容托管服务?

javascript - 检测div中点击的水平位置

javascript - 如何在 Angular 中获取复选框项的值

javascript - 尝试分析为什么这种转变没有发生