为什么在我的指令中未将以下范围变量变为未定义?

时间:2016-01-27 01:25:36

标签: javascript angularjs

我正在尝试获取panodatas的值(在我的控制器中定义为$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确实有值:

[
  {
    "objectId": "56a74de0d342d30054170231",
    "index": 2,
    "x": 200,
    "y": 200,
    "createdAt": "2016-01-26T10:43:44.482Z"
  },
  {
    ...
  },
  ...

修改

这就是我的控制器中panodatas的定义方式:

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
  })
}

1 个答案:

答案 0 :(得分:1)

因为在使用panodatas返回promise之前运行链接器。