加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 在指令内使用$http和$templateCache不返回结果

发布时间:2020-12-17 09:28:58 所属栏目:安全 来源:网络整理
导读:我正在尝试创建一个加载模板的指令.然后,该模板将被缓存,因此第二次单击不会尝试加载的元素,而是从$templateCache获取最近加载的值. 我注意到,在缓存命中的情况下,我没有从$http.get()方法得到任何响应. html ng-app="website"body ng-controller="MyControl
我正在尝试创建一个加载模板的指令.然后,该模板将被缓存,因此第二次单击不会尝试加载的元素,而是从$templateCache获取最近加载的值.

我注意到,在缓存命中的情况下,我没有从$http.get()方法得到任何响应.

<html ng-app="website">
<body ng-controller="MyController">
    <a href='#' ng-click="load()">Click Event</a>
    <a href='#' click-load>Click Directive</a>

</body>
</html>?

angular.module('website',[]).directive('clickLoad',function($q,$http,$templateCache) {
    return function(scope,element,attrs) {
        function loadData() {
            $http.get('http://fiddle.jshell.net',{
                cache: $templateCache
            }).then(function(result) {
                alert('loaded ' + result.data.length + " bytes");
            });

        }
        element.bind('click',loadData);
    };
});


function MyController($scope,$templateCache) {
    $scope.load = function() {
        $http.get('http://fiddle.jshell.net',{
            cache: $templateCache
        }).then(function(result) {
            alert('loaded ' + result.data.length + " bytes");
        });
    }
}

我创建了一个模拟我的场景的小提琴:

http://jsfiddle.net/3ea64/

请注意,您可以按需要点击事件链接多次,但是,如果您单击“单击指令”链接,只能单击“仅点击”一次,如果单击“点击事件”链接,则该链接不起作用.

任何想法都不胜感激.

我已经玩了一下,使用了AngularJS的缓存(在这里描述: http://docs.angularjs.org/api/ng. $http)

这是一个现场演示:http://jsfiddle.net/4SsgA/

我基本上调整了$http语法,并使用ng-click指令,而不是在指令内注册一个事件监听器(只因为我更喜欢它:))

HTML:

<html ng-app="website">
<body ng-controller="MyController">
    <a href='#' ng-click="load()">Click Event</a>
    <a href='#' click-load ng-click="loadData()">Click Directive</a>
</body>
</html>?

JS:

angular.module('website',attrs) {
        scope.loadData = function() {
            $http({method: 'GET',url: 'http://fiddle.jshell.net',cache: true}).then(function(result) {
                alert('loaded ' + result.data.length + " bytes");
            });
        }
    };
});


function MyController($scope,$templateCache) {
    $scope.load = function() {
        $http({method: 'GET',cache: true}).then(function(result) {
            alert('loaded ' + result.data.length + " bytes");
        });
    }
}?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读