Reputation: 100
This is how if I have not written any contents into my two textbox will see it with errors are: undefined and it comes first away when you've written something content.
I've written code like this:
Default.aspx
<div ng-app="Welcomecontroller" ng-controller="FullName">
<label>Name:</label>
<input type="text" ng-model="Fornavn" placeholder="Enter a name here">
<input type="text" ng-model="Efternavn" placeholder="Enter a name here">
<hr>
<h1>{{fullName()}}</h1>
</div>
WelcomeController.js
var Welcome = angular.module('Welcomecontroller', []);
Welcome.controller('FullName', function ($scope) {
$scope.fullName = function () {
return "Velkommen til " + $scope.Fornavn + " " + $scope.Efternavn;
}
});
when it has no content will see it and write like this:
Velkommen til undefined undefined
However, I also tried to do so to check up on that content was empty when it was.
WelcomeController.js
var Welcome = angular.module('Welcomecontroller', []);
Welcome.controller('FullName', function ($scope) {
if ($scope.Fornavn != "" && $scope.Efternavn != "") {
$scope.fullName = function () {
return "Velkommen til " + $scope.Fornavn + " " + $scope.Efternavn;
}
}
else
{
return "Test";
}
});
Upvotes: 1
Views: 43
Reputation: 191749
You can use ng-if
in the template itself to check.
<div ng-app="Welcomecontroller" ng-controller="FullName as fullname">
<label>Name:</label>
<input type="text" ng-model="fullname.Fornavn" placeholder="Enter a name here">
<input type="text" ng-model="fullname.Efternavn" placeholder="Enter a name here">
<hr>
<h1 ng-if="fullname.Fornavn && fullname.Efternavn">
Velkommen til {{fullname.Fornavn}} {{fullname.Efternavn}}</h1>
<h1 ng-if="!fullname.Fornavn || !fullname.Efternavn">Test</h1>
</div>
In this case you actually don't even need anything in the controller.
Upvotes: 2