gpt4 book ai didi

AngularJS 指令在范围内创建空数组。

转载 作者:行者123 更新时间:2023-12-02 09:35:19 26 4
gpt4 key购买 nike

我创建了一个指令并以这种方式使用范围变量:

<div data-create-instants-since-beginning data-daysnum="daysnum" data-tostore="tostore">

我希望当 tostore 未定义时将其设置为空数组,但不幸的是这不起作用。

.directive("createInstantsSinceBeginning", function () {
return {
scope: {
daysnum: "=",
tostore: "="
},
link: function ($scope, element) {
if (typeof $scope.tostore == 'undefined') {
$scope.$apply(function () {
$scope.tostore = [];
});
}
// other code ..

我该如何解决这个问题?

最诚挚的问候。

最佳答案

尝试取出$scope.apply

.directive("createInstantsSinceBeginning", function() {
return {
scope: {
daysnum: "=",
tostore: "="
},
link: function(scope, element) {
if (typeof scope.tostore == 'undefined') {
scope.tostore = [];
}
}
};
});

参见工作示例:http://plnkr.co/edit/OVsKcDebdNhxgCfdig4q?p=preview

关于AngularJS 指令在范围内创建空数组。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27040738/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com