LoginSignup
2
6

More than 5 years have passed since last update.

AngularJSでデータが入力されている場合に要素を表示する方法

Last updated at Posted at 2015-07-24

ng-showの利用

ng-showを使ってHTML要素の表示を制御していきます。

<!DOCTYPE html>
<html ng-app>
<head>
    <meta charset="utf-8">
    <title>practice Angular.js</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
</head>
<body>
    <div>
        <label>Name:</label>
        <input type="text" ng-model="yourName" placeholder="Enter a name here">
        <hr>
        <h1 ng-show="!!yourName">Hello, {{yourName}}!</h1>
    </div>
</body>
</html>

まず、テキストボックスに入力したテキストは、yourNameという変数に入ります。

次に、ng-show="!!yourName"の部分で、yourNameが空ではない場合に当該の要素を表示するようにしています。これでテキストボックスに何かしらのテキストが入力されると、Hello, {{yourName}}!が表示されるようになります。

また、一度入力した文字をすべて消去すると、再度表示されなくなります。

2
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6