简体   繁体   English

AngularJS指令和隔离范围

[英]AngularJS Directive and Isolate Scope

I am trying to understand how to pass data through my directive so that I can bind it at the view level. 我试图了解如何通过我的指令传递数据,以便我可以在视图级别绑定它。 I believe I understand the separation of controller scope vs. the directive isolate scope but I cant seem to get a simple json result out to my view. 我相信我理解控制器范围与指令隔离范围的分离,但我似乎无法得到一个简单的json结果。 My JSFiddle can be found here http://jsfiddle.net/jamesamuir/2KLVj/4/ . 我的JSFiddle可以在http://jsfiddle.net/jamesamuir/2KLVj/4/找到。

app.directive('testList', function (testService) {
    return {
        restrict: 'A',

        link: function ($scope, element, attrs) {

            $scope.name = 'isolate scope';
            $scope.data = {};
            $scope.data.loadtext = testService.getJSON().then(function (data) {
                alert(data);
                element.addClass("red");

            });
        }
    }
});

It seems to me that this should work but, alas, it does not. 在我看来,这应该工作,但是,唉,它没有。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Inside your then() callback, assign data to data.loadtext : then()回调中,将data分配给data.loadtext

testService.getJSON().then(function (data) {
   alert(data);
   element.addClass("red");
   scope.data.loadtext = data;
});

fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM