angularjs观察动态生成的范围对象

时间:2014-11-21 02:48:11

标签: angularjs watch

我需要根据范围属性动态添加观察程序。

下面的代码作为硬编码工作,但如果我尝试从范围中获取值,它就不起作用。我已多次测试,在此示例中:

$scope.options.hideExpression // "hideDefault"
$scope.options.default_to // "allowed_item_types"



if $scope.options.default_to
  defaultTo = $scope.options.default_to
  $scope.$watch 'model["allowed_item_types"]', (newVal) -> // Hard coded, works
  # $scope.$watch 'model[defaultTo]', (newVal) -> // Dynamically generated from scope, doesn't work
    if Object.count($scope.model[$scope.options.default_to], /true/) == 1 // Dynamically generated from scope, works
      # Set default value
      $scope.model[$scope.options.key] = Object.find(newVal, /true/) // Dynamically generated from scope, works
      # Hide default row
      # $scope[$scope.options.hideExpression] = true // Dynamically generated, doens't work
      $scope["hideDefault"] = true  // Hard coded, works,
    else
      # $scope[$scope.options.hideExpression] = false
      $scope["hideDefault"] = false
  , true

知道为什么会这样吗?

由于某种原因,这个特定的行是唯一一个不能硬编码的行: $ scope.model [$ scope.options.default_to]

0 个答案:

没有答案