angularjs - 在指令的隔离范围内绑定的对象在范围结构中,但在访问时未定义
问题描述
HTML
<div class="block-area" id="basic" ng-controller="DocumentEditorController">
<docs-hierarchy table-of-contents="document.toc" document-id="123123123">
</docs-hierarchy>
</div>
指示
.directive('docsHierarchy', [function() {
return {
restrict: 'E',
scope: {
tableOfContents: '=',
documentId: '@',
},
template: '' +
'<docs-hierarchy-tree-render tree="tree"></docs-hierarchy-tree-render>' +
'',
link: function($scope, elem, attrs) {
$scope.meta = {};
$scope.tree = {};
function parseTree() {
console.log("TOC", "$scope:", $scope);
console.log("TOC", "$scope.tableOfContents:", $scope.tableOfContents);
console.log("TOC", "$scope.tree:", $scope.tree);
console.log("TOC", "$scope.documentId:", $scope.documentId);
}
parseTree();
}
简单地说,我不明白值输出中的值如何存在$scope
,并立即成为undefined
下一行。为什么我无法获得已经评估的这个值?
但我可以很好地抓住字符串绑定。
编辑 01
我设法弄清楚了这样做的第一种方式,如下所示:
.directive('docsHierarchy', [function() {
return {
restrict: 'E',
scope: {
tableOfContents: '=',
documentId: '@',
},
template: '' +
'<docs-hierarchy-tree-render tree="tree"></docs-hierarchy-tree-render>' +
'',
link: function($scope, elem, attrs) {
$scope.meta = {};
$scope.tree = {};
function parseTree() {
console.log($scope.toc);
}
$scope.$watch('tableOfContents', function(newVal, oldVal) {
if(newVal !== oldVal) {
$scope.toc = newVal;
parseTree();
}
});
}
};
}])
我仍然不明白为什么我必须这样做,如果tableOfContents
据说是数据绑定
解决方案
推荐阅读
- linux - udev - += 和 := 之间的区别
- transactions - 通过超级账本结构中的交易 ID 获取发件人证书
- mysql - 如何使用MYSQL返回与用户关联的所有对象也都在另一个表中
- mongodb - 机器上的 MongoDB 或 MongoDB Atlas
- ios - 针对 docker 实例开发 iOS 应用程序有什么好处吗?
- sockets - 如何在 JMeter 中发送一些 TCP 十六进制数据包
- python-3.x - 我需要在发票模型中显示合并的日期字段,特别是在发票模型的 date_invoice 字段中
- c++ - 如何在VS项目中静态链接没有dll文件的cpprest?
- java - Python 调用的服务执行程序问题
- css - 根据数据库中的值更改 css