2016-08-08 18 views
2

ng-initにチェックundefinedの値を追加できますか?スコープに値がある場合は、それ以外のものを印刷してください。foobar。現在、それは印刷する必要がありますfoobar ng-initに条件を追加することはできますか?あなたはJavascript式/ラインで書くようAngularJSで未定義をチェックする方法は?

<body ng-controller="MainCtrl" ng-init="test = test|| 'aasasas'"> 
    <p>Hello {{test}}!</p> 
</body> 

あなたはほとんどすべてを書くことができます。

は、ここではそれは簡単なことだ私のコード http://plnkr.co/edit/bFbAWXq6dKdkxWAxp7v8?p=preview

<body ng-controller="MainCtrl" ng-init="test|| 'foobar'"> 
    <p>Hello {{test}}!</p> 
    </body> 
+1

は、基本的なJavaScriptの構文を使用し、その定義されている場合testの値は、それ自体testなりますng-init

<body ng-controller="MainCtrl" ng-init="test = test ? test: 'aasasas'"> <p>Hello {{test}}!</p> </body> 

に条件を追加することができます - http://stackoverflow.com/questions/3390396/how-to-check-for-undefined-in-javascri pt – cpoDesign

答えて

1

です。

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

 
app.controller('MainCtrl', function($scope) { 
 

 
});
<script data-require="[email protected]" src="http://code.angularjs.org/1.2.0rc1/angular.js" data-semver="1.0.7"></script> 
 
<div ng-app="plunker" ng-controller="MainCtrl" ng-init="test = test|| 'aasasas'"> 
 
    <p>Hello {{test}}!</p> 
 
</div>

0

はい、あなたがそうでなければ 'aasasas'

関連する問題