gpt4 book ai didi

javascript - 在 Controller 中使用 ajax 请求的 AngularJS 指令中未定义范围

转载 作者:行者123 更新时间:2023-11-30 10:23:45 25 4
gpt4 key购买 nike

我使用 AngularJS 大约一周了,但我遇到了一个我无法真正理解的自定义指令的问题。我的主要目标是在一个指令中制作一个 html 表,其中 json 数据通过 $http 服务加载到 Controller 中。

我有一个 View 模板。如果我使用像 ng-repeat 或表达式这样的 Angular 指令,数据似乎已正确加载并绑定(bind)到范围,并且我可以呈现我的 View 。

但是如果我在文档根目录下使用自定义指令,它会在 Controller 发送请求之前触发。因此,当我在链接函数 scope.myData 中使用范围时,它是未定义的。但是如果我在 ng-repeat 中使用自定义指令,我可以访问本地范围。我不明白为什么 Angular 指令会在数据加载后触发,为什么我的指令会在加载前触发。我错过了什么吗?

实际上,我的数据确实比示例更复杂,我必须在自定义指令中分析它们以生成我的 html 表:为示例数据中的某些属性(如组名)制作 rowspan 或 colspan。

任何帮助都是有用的,在此先感谢。

这是我的示例代码。

app.js

    angular.module('myApp', [
'ngRoute',
'myApp.filters',
'myApp.services',
'myApp.directives',
'myApp.controllers'
]).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
$routeProvider.when('/test', {templateUrl: 'partials/test.html', controller: 'TestCtrl'});
$routeProvider.otherwise({redirectTo: '/view1'});
}]);

controllers.js

angular.module('myApp.controllers', []).

controller('TestCtrl',['$scope', '$http',function($scope, $http){
$http.get('data/test.json').success(function(data) {
$scope.myData = data;
});

}]) ;

test.html

<!-- this works perfectly -->
<h3>{{myData.title}}</h3>
<table class="table table-condensed table-bordered">
<thead>
<tr>
<th ng-repeat="col in myData.cols">{{col.title}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in myData.rows">
<td>{{row.group}}</td>
<td ng-repeat="col in row.cols">{{col}}</td>
</tr>
</tbody>
</table>
<!-- does not work ! -->
<table test-table></table>

directives.js

angular.module('myApp.directives', []).
.directive('testTable', ['$compile', function(compile){
return{
link: function(scope,elm,attrs,ctrl){
for(var i = 0, l= scope.myData.rows.length; i<l; i++){
var tr = angular.element(['<tr>', '</tr>'].join(''));
console.log(tr);
for(var j = 0; j<scope.myData.cols.length; j++){
var td = angular.element(['<td>', String(scope.myData.rows[i].cols[j]), '</td>'].join(''));
tr.append(td);
}
elm.append(tr);

}
compile(elm.contents())(scope);
}
}
}]);

test.json

{
"title" : "This is a simple sample data.",
"cols" : [{"title":"Group Name"},{"title":"Col1"},{"title":"Col2"},{"title":"Col3"}],
"rows" : [
{"group" : "group A","cols":["Something","Something else","Other stuff"]},
{"group" : "group A","cols":["Something","Something else","Other stuff"]},
{"group" : "group A","cols":["Something","Something else","Other stuff"]},
{"group" : "group B","cols":["Something","Something else","Other stuff"]},
{"group" : "group B","cols":["Something","Something else","Other stuff"]}
]

}

最佳答案

有几种方法可以做到这一点:

  1. 您可以尝试使用 resolve 路由参数(在 docs 中找到它)。这将延迟加载页面,直到 promise 得到解决。

  2. 您像使用 jQuery 一样使用 Angular - 不要那样做。 (这是 Angular 新手的常见问题 - 阅读 this great answer。)关于 ng-repeat 如何看到变化的问题:简而言之,ng-repeat 观察作用域变量并对变化采取行动。你可以做类似的事情:

    scope.$watch("myData", function(newval) { ...});

如果你想设置 rowspan-colspan,ng-repeat 的扩展语法可能会有所帮助:

<header ng-repeat-start="item in items">
...
<footer ng-repeat-end>

(查看长示例 here )

关于javascript - 在 Controller 中使用 ajax 请求的 AngularJS 指令中未定义范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20516064/

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