指令不响应范围变量的变化

时间:2014-01-01 23:42:51

标签: javascript angularjs

我有以下指令:

  <span ng-show="{{ save_state == 'saved' }}"> Saved </span>
  <span ng-show="{{ save_state == 'saving' }}"> Saving </span>
  <span ng-show="{{ save_state == 'error' }}"> Error </span>

这些回调在不同的时间被调用:

var saving = function() {
    $scope.save_state = "saving";
    $scope.$apply();
};
var saved = function() {
    $scope.save_state = "saved";
    $scope.$apply();
};
var error = function(err) {
    alert(err);
    $scope.save_state = "error";
    $scope.$apply();
};

我也有此声明用于调试

<span> {{ save_state == 'saving' }} </span>

出于某种原因,即使上面的span标记显示true,“保存”范围标记也不会显示。 “错误”也是如此。为什么?

1 个答案:

答案 0 :(得分:1)

这是因为您正在插入{{ }} ng-show指令的内容:

ng-show="{{ save_state == 'saved' }}"

ng-show要求您提供有效的Angular Expression。:

ng-show="save_state == 'saved'"