当样式显示为none时,ng-model未设置为输入颜色

时间:2017-11-09 15:22:14

标签: javascript css angularjs

我有这个html(下面),对于输入颜色类型,我想显示Font Awesome图标而不是输入,哪个工作正常,问题是通过将输入的显示设置为none,模型没有使用新颜色更新,如果我删除显示设置,这可以正常工作。有什么建议吗?

<label for="color{{id_form_group}}" class="fa-stack">
        <i class="fa fa-square-o fa-stack-2x"></i>
        <i class="fa fa-paint-brush fa-stack-1x" style="color:{{objeto.style.color}};"></i>
        <input id="color{{id_form_group}}" ng-model="objeto.style.color" type="color" style="display:none;">
</label>

3 个答案:

答案 0 :(得分:0)

好的将css改为style="visibility:hidden;"而不是style="display:none;"就可以了,我不知道为什么,如果有人有更好的答案或解释为什么会发生这种情况,请发布并发布我会将你的答案标记为正确。

答案 1 :(得分:0)

您不必对angularjs使用 display:none ,因为它提供 ng-hide ng-show 指令

这应该可以解决问题,

<input id="color{{id_form_group}}" ng-model="objeto.style.color" type="color" ng-hide="true"">

答案 2 :(得分:0)

您可能需要发布其余的代码,因为我可以使用您的代码段来使其正常工作:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.objeto = {
    style: {
      color: ""
    }
  }
});
<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
  <script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
  <p>Color: <span style="font-weight: bold; color: {{objeto.style.color}}">{{objeto.style.color}}</span></p>
  <form id="my-form">
    <label for="color{{id_form_group}}" class="fa-stack">
      <i class="fa fa-square-o fa-stack-2x"></i>
      <i class="fa fa-paint-brush fa-stack-1x" style="color:{{objeto.style.color}};"></i>
      <input id="color{{id_form_group}}" ng-model="objeto.style.color" type="color" style="display: none" />
    </label>
  </form>
</body>

</html>