简体   繁体   English

Angular JS - 指令中的数据绑定不起作用

[英]Angular JS - Data binding in directive is not working

I have an issue with a data binding inside a directive, which call another directive. 我在指令中有一个数据绑定问题,它调用另一个指令。

Here is the main directive : 这是主要指令:

var app = angular.module('app');

app.directive("myMainDirective", function ($http) {
return {
    scope: {
        paramId: '='
    },
    link: function (scope) {
        $http.get('some/url/' + scope.paramId+ '.json'
        ).success(function (data) {
                scope.idFromServer = data;
            });
    },
    template: '<span other-directive id="idFromServer"></span>'
}
});

Here is the other directive : 这是另一个指令:

var app = angular.module('app');

app.directive("otherDirective", ['$http', function(http) {
return {
    template: "<span>{{name}}</span>",
    scope: {
        id: "="
    },
    link: function(scope) {
        http.get('another/url/' + scope.id + '.json').then(function(result) {
            scope.name = result.data.name;
        }, function(err) {
            scope.name = "unknown";
        });
    }
}
}])

And the html code wich call the main directive : 以及调用主要指令的html代码:

<span my-main-directive param-id="myObject.id"></span>

When calling "other-directive", the "idFromServer" is not bind, and is "undefined", so it results to diplay "undefined". 当调用“other-directive”时,“idFromServer”不绑定,并且是“未定义”,因此它会导致显示“未定义”。

I'm probably missing something stupid, but I don't see what ... (My piece of code is probabley not the best, I'm pretty new to angularjs, and specially the directives, and I tried a lot of ways to accomplish what I want.) 我可能错过了一些愚蠢的东西,但我看不出是什么......(我的代码很可能不是最好的,我对angularjs很新,特别是指令,我尝试了很多方法实现我想要的。)

Per my comments, here's one way that might work, using a scope.$watch: 根据我的评论,这是使用范围可能有效的一种方式。$ watch:

scope.$watch('id', function(id) {
    $http.get('some/url/' + id + '.json')
        .success(function (data) {
            scope.idFromServer = data;
        });
};

This would go inside the link function on the nested directive. 这将进入嵌套指令的link函数内部。

One of the way I'd suggest is don't use two way( = ) binding on idFromServer variable, use {{idFromServer}} Interpolation directive to assign value to attribute, & then use $attr.$observe it will call when interpolation is evaluated. 我建议的方法之一就是不要在idFromServer变量上使用双向( = )绑定,使用{{idFromServer}}插值指令为属性赋值,然后使用$attr.$observe它会在插值时调用被评估。

myMainDirective myMainDirective

app.directive("myMainDirective", function ($http) {
return {
    scope: {
        paramId: '='
    },
    link: function (scope) {
        $http.get('some/url/' + scope.paramId+ '.json'
        ).success(function (data) {
                scope.idFromServer = data;
            });
    },
    template: '<span other-directive id="{{idFromServer}}"></span>'
}
});

otherDirective otherDirective

app.directive("otherDirective", ['$http', function(http) {
    return {
        template: "<span>{{name}}</span>",
        scope: true, //isolated scope
        link: function(scope, element, attr) {
            attr.$observe(attr.id, function(newVal) {
                http.get('another/url/' + newVal + '.json').then(function(result) {
                    scope.name = result.data.name;
                }, function(err) {
                    scope.name = "unknown";
                });
            });
        }
    }
}])

Since javascript is asynchronous, your two ajax requests are running at basically the same time and id is undefined when the request in other-directive runs. 由于javascript是异步的,因此两个ajax请求基本上同时运行,并且当other-directive的请求运行时, id undefined

If you want to try testing this, just set a default value for idFromServer . 如果您想尝试测试,只需为idFromServer设置默认值。 The request in other-directive will run with the default value. other-directive的请求将以默认值运行。

EDIT: in response to your comment, that is quite a broad question and there are many solutions. 编辑:回应你的评论,这是一个非常广泛的问题,有很多解决方案。 The best answer I can give you is simply, never ever run any logic in your link function, just define the directive's behavior and properties - that's what the link function is for. 我能给你的最好答案就是,从来没有在你的链接函数中运行任何逻辑,只需定义指令的行为和属性 - 这就是链接函数的用途。 The template is used right away and you can't change that. 模板立即使用,您无法更改。

In this case, you could get the data prepared in a parent scope and pass the data in attributes. 在这种情况下,您可以获取在父作用域中准备的数据并在属性中传递数据。

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

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