angularjs - In an Angular template, is there a way to throw an error if a variable is undefined? -


in angular templates, if there undefined variable in template, fail silently.

some sample code below:

the controller:

angular.module('app').controller('myctrl', function($scope) {   //nothing here }); 

and view:

<div ng-controller="myctrl">   <p ng-click="dosomething()">{{ data }}</p>   <input type="text" ng-modal="input"> </div> 

none of variables interpolated because don't exist on $scope. mistyped ng-modal (not ng-model) attribute fail silently. undesirable behavior in development.

is there way change errors thrown? i'd okay monkey-patch solution angular this.

you can make sure value selected, using || in template. when using || assign variable, first truthy variable returned. in example:

<div ng-controller="myctrl">   <p ng-click="dosomething()">{{ data || "data not defined" }}</p>   <input type="text" ng-modal="input"> </div> 

this have message change "data not defined" $scope.data whenever data becomes defined. if want error, use same approach instead of string, use function.

<div ng-controller="myctrl">   <p ng-click="dosomething()">{{ data || handleemptydata(data) || "data not defined , handleemptydata() returned undefined" }}</p>   <input type="text" ng-modal="input"> </div>  $scope.handleemptydata = function(data){     if(!data){ alert("error, data " + json.stringify(data)); } }; 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -