我正在尝试一些 angularJS 开发,但我遇到了一个我根本不理解的问题。
我尝试将 JSON 格式化为具有许多规则的特定表。现在我的 JSON 看起来像:
{
"id":"test",
"title":"Test Sample",
"description":"Test Sample Description"
}
它被加载到list
:
angular.module('myServices', ['ngResource'])
.factory('MyData', ['$resource',
function($resource){
return $resource('/rest/data', {}, {
getSample: {method:'GET', url:'/data/sample.json', isArray:false}
});
}
]);
angular.module('myControllers', ['myServices'])
.controller('DataController', ['$scope', 'MyData',
function($scope, MyData){
$scope.list = MyData.getSample(); //retrive a simple JSON
}
]);
然后我尝试重新排列数据:
<div id="list" data-ng-controller="DataController">
<div data-table="view" data-list="list"></div>
</div>
现在是一个简单的指令:
angular.module('myDirectives', [])
.directives('table', function($timeout){
return {
restrict: 'A',
replace: true,
scope: { list : '='},
link: function(scope,$element, attrs){
console.log(scope);
console.log(scope.list);
console.log(scope.list.title);
}
};
});
但这里发生了什么console.log()
:我有scope
:
$$asyncQueue: Array[0]
$$childHead: null
$$childTail: null
$$destroyed: false
$$isolateBindings: Object
$$listeners: Object
$$nextSibling: null
$$phase: null
$$postDigestQueue: Array[0]
$$prevSibling: null
$$watchers: Array[1]
$id: "007"
$parent: a
$root: h
list: Resource
this: h
__proto__: h
我有scope.list
(标记为Resource
):
$promise: Object
$resolved: true
description: "Test Sample Description"
id: "test"
title: "Test Sample"
__proto__: Resource
但scope.list.title
被标记为undefined
。
我的搜索导致某些属性可能尚未在链接中设置,但为什么scope.list
仍然可用而不是它的属性?
我也尝试将 to 设置为scope
指令,但没有更好的事情发生true
table
我即将制作一个复杂的表格,所以我必须在这个 div 和表格中应用非常具体的规则。我不能简单地用一些ng-If
.