我正在尝试获取 panodatas
的值(在我的 Controller 中定义为$scope.panodatas
)并在link
中使用它我的指令部分:
JS:
scope: {
floorplan: "@",
points: "=",
panodatas: "=",
getPanos: "&"
},
link: function(scope, element, attrs) {
console.log(scope.panodatas)
scope.getPanos().then(function() {
console.log(scope.panodatas)
HTML:
<pre>{{panodatas | json}}</pre>
<panodata-editor
floorplan="http://ac-x69r2ohx.clouddn.com/3171600a0de6f740c152.jpg"
points="points"
panodatas="panodatas"
get-panos="getPanos()"
>
</panodata-editor>
但是这不起作用,两个 console.log(scope.panodatas)
返回undefined
。为什么是这样?我怎样才能得到$scope.panodatas
的值并在 link: function() {...}
中使用它?
注意:
<pre>{{panodatas | json}}</pre>
正在工作,所以 panodatas
确实有这样的值(value):
[
{
"objectId": "56a74de0d342d30054170231",
"index": 2,
"x": 200,
"y": 200,
"createdAt": "2016-01-26T10:43:44.482Z"
},
{
...
},
...
编辑:
这就是panodatas
正在我的 Controller 中定义:
var all = function() { return $q.all([serviceUpload.getAllBuilding(isUpdated)]) }
$scope.getPanos = function() {
return all().then(function(value) {
$scope.buildingTree = JSON.parse(value)
$scope.panoId = $routeParams.panoId
$scope.pano = _($scope.buildingTree).chain().
pluck('pano').
flatten().
findWhere({objectId: $scope.panoId}).
value()
$scope.panodatas = $scope.pano.panoData
}, function(reason) {
$scope.result = reason
})
}
最佳答案
因为链接器运行时是在 promise 随 panodatas 返回之前。
关于javascript - 为什么以下范围变量在我的指令中没有变为未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35027226/