編集:フィルターでそれを行う...(グローバルになります)
app.filter('undefined', function(){
return function(input, message) {
return angular.isDefined(input) ? input : message;
};
});
使用する:
<div ng-controller="MyController">
{{oops | undefined:'ERROR: No such name: "oops"'}} <!-- This name does not exist in my $scope -->
</div>
これでうまくいくはずです。
これは、それを行うための迅速かつ簡単な方法です...
HTML
<div ng-controller="MyController">
<span ng-show="isDefined(oops)">{{oops}}</span><span ng-hide="isDefined(oops)">ERROR: No such name: "oops"</span>
</div>
コントローラーで:
app.controller("MyController", function($scope) {
$scope.isDefined = function(x) {
return angular.isDefined(x);
};
});
編集 2:すべてを「自動的に」実行する真の「グローバル」アプローチ...そのためには、Angular の ngBind ディレクティブを書き換えるか、独自のバインディング ディレクティブを作成してどこでも使用することを検討しています。
これは Angular の ngBind ディレクティブで、ここの 50 行目にあります。
var ngBindDirective = ngDirective(function(scope, element, attr) {
element.addClass('ng-binding').data('$binding', attr.ngBind);
scope.$watch(attr.ngBind, function ngBindWatchAction(value) {
element.text(value == undefined ? '' : value); //<-- this is the line.
});
});
ご覧のとおり、値が定義されていない場合はデフォルトで '' になります。