2015-02-23 108 views
0
後,我已經改變了輸入值

修改例如工作正常 - 最初隱藏/顯示做我想要的正好相反? (你好「」)
我需要運行某種on-ready-go()嗎?angularJS隱藏/顯示最初

<!doctype html> 
<meta http-equiv="Content-type" content="text/html;charset=UTF-8"> 
<html ng-app> 
<head> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js"></script> 
</head> 
<body> 
    <div> 
     <input type="text" ng-model="yourName" placeholder="Enter your name"> 
     <hr> 
     <h1 ng-show="yourName == ''">Who ARE you ?</h1> 
     <h1 ng-hide="yourName == ''">Hello "{{yourName}}"</h1> 
    </div> 
</body> 
</html> 
+0

你有沒有在控制器初始化'yourName'的東西?還是=未定義? – 2015-02-23 08:55:57

+0

我不能看到任何'NG-app' /'NG-controller'在HTML – 2015-02-23 08:56:52

+0

就在今天,在angularJS開始 - 上面的代碼是所有有.. – T4NK3R 2015-02-23 08:58:59

回答

2

可以使用ng-init這裏

<input type="text" ng-model="yourName" placeholder="Enter your name" ng-init="yourName = ''"> 

這裏是Demo Plunker

OR使用

<div> 
    <input type="text" ng-model="yourName" placeholder="Enter your name"> 
    <hr> 
    <h1 ng-show="!yourName">Who ARE you ?</h1> 
    <h1 ng-hide="!yourName">Hello "{{yourName}}"</h1> 
</div> 

​​3210

問題

yourName範圍變量undefined,直到該值第一次更改。如果您只更改一次值,則會有一個名爲yourName的範圍變量。你可以看到,如果你的類型,然後退格鍵,文本框Who ARE you ?將打印,因爲在這一點上有一個名爲yourName變量。

您可以使用ng-init初始化變量,然後在範圍內有一個名爲yourName的變量。所以yourName不是undefined

作爲第二個例子!yourName,如果yourNameundefined''!yourName成爲真。

+0

啊!「YOURNAME」 - 所以這就是他們所說的「truthy」當評價:) – T4NK3R 2015-02-23 09:05:15

+0

和NG-INIT取代值=「布拉布拉」 - 猜猜這angularBusiness改變很多東西:) – T4NK3R 2015-02-23 09:16:31

0

這是因爲在默認情況下yourName的是undefined。像這樣嘗試。

<h1 ng-hide="yourName == '' || yourName == undefined ">Hello "{{yourName}}"</h1> 

<!doctype html> 
 
<meta http-equiv="Content-type" content="text/html;charset=UTF-8"> 
 
<html ng-app> 
 
<head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js"></script> 
 
</head> 
 
<body> 
 
    <div> 
 
     <input type="text" ng-model="yourName" placeholder="Enter your name"> 
 
     <hr> 
 
     <h1 ng-show="yourName == ''">Who ARE you ?</h1> 
 
     <h1 ng-hide="yourName == ''|| yourName == undefined ">Hello "{{yourName}}"</h1> 
 
    </div> 
 
</body> 
 
</html>

+0

對不起 - 1分鐘後期,隨着相同的答案(減ng-init) – T4NK3R 2015-02-23 09:18:56