仅在定义{{variable}}时呈现

时间:2014-12-11 05:01:31

标签: angularjs angularjs-bootstrap

说我有一些文字,如:

The data is ready. Here it is: {{something}}

如果something不是undefined,我只想渲染该行。我怎么能这样做?

如果有帮助,这就是更广泛背景下的那条线:

<html>
<body>
<div>
<div class='container-fluid' ng-controller="TypeaheadCtrl">

<input type="text" ng-model="selected" 
typeahead="name_and_uid as item.name_and_uid for item in items | 
filter:{name_and_uid: $viewValue} | limitTo:16" 
typeahead-on-select='onSelect($item, $model, $label)' class="form-control">

The data is ready. Here it is: {{name_and_uid}}

</div>
</body>
</html>

其中{{name_and_uid}}仅在选择完成后定义。

目前,我在做出选择之前得到"The data is ready. Here it is:"。我只想在输入并进行选择后进行渲染。

2 个答案:

答案 0 :(得分:2)

您可以在您的范围内定义:

   $scope.isDefined = function(v) {
            return angular.isDefined(v);
        };

然后

<div ng-show="isDefined(name_and_uid)">
    The data is ready. Here it is: {{name_and_uid}}
</div>

答案 1 :(得分:0)

假设输入ng-model长度最初为0,您可以使用ng-show仅在$scope.selected大于零时呈现该行。

<div ng-show="selected.length > 0">
    <span>The data is ready. Here it is: {{name_and_uid}}</span>
</div>