Readonly在AngularJs中不起作用

时间:2016-03-04 00:38:47

标签: angularjs

在我的HTML文件中:

<rating ng-show = "onerate =='null'" readonly="isReadOnly" ng-model="newRate.rating" max="max" class="assertive" ng-click="addRating('{{singleRecipe[0].recipe_id}}')" >
</rating>

我希望每当我点击一次它就永远不会被点击。

controller文件中的Javascript内:

$scope.rate = 1;
$scope.max = 5;
$scope.isReadonly = false;
$scope.newRate = {};
$scope.newRate.userID = $scope.userdata.user_id;
$scope.addRating = function(recipe_id){
   $scope.newRate.recipeID = recipe_id;
   RatingList.add($scope.newRate);
   console.log($scope.newRate);
   $scope.isReadonly = true;
}

2 个答案:

答案 0 :(得分:1)

您应该使用ng-readonly docs here

请记住,readonly属性仅适用于输入对象,如果要创建响应click的元素,则必须以不同方式处理它。

您真正想要的是更改点击方式:

$scope.addRating = function(recipe_id){
  if (!$scope.isReadonly)
  {
    $scope.newRate.recipeID = recipe_id;
    RatingList.add($scope.newRate);
    console.log($scope.newRate);
    $scope.isReadonly = true;
  }
}

您使用ng-class$scope.isReadonly动态添加类,以便直观地显示该元素不再可点击。更多信息here

答案 1 :(得分:0)

使用 ng-readonly

https://docs.angularjs.org/api/ng/directive/ngReadonly

您现在只有只读