1

I want the first typed letter turn into a capital letter, but struggeling with Angular. Maybe it is a little detail that I missed out, so a fresh eye to look at this would be helpful.

var app = angular.module("todoApp", []);
  app.filter("NameInput", function(){
    return function(input, scope){
     if (input!=null)
     input = input.toLowerCase();
     return input.substring(0,1).toUpperCase()+input.substring(1);
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<body ng-app="todoApp" ng-cloak>
    <div id="wrapper">

    <div ng-controller="NameInput">
      <label>Name:</label>
      <input id="name-input" type="text" ng-model="yourName" placeholder="Enter a name here">
      <hr>
      <h1>Hello {{yourName}}</h1>
    </div>
    </div>
 </body>

6
  • as you are using NameInput controller on view, have you defined it ? Commented Sep 25, 2016 at 19:01
  • where is your controller? Commented Sep 25, 2016 at 19:07
  • <div ng-controller="NameInput"> Commented Sep 25, 2016 at 19:11
  • where is the controller "NameInput" is not defined in the code please update it Commented Sep 25, 2016 at 19:16
  • That is exactly my point, do not know where I should look at. Commented Sep 25, 2016 at 19:19

1 Answer 1

1

Here you go... I think this might help you

var app = angular.module("todoApp", []);
app.filter("NameInput", function() {
    return function(input) {
      if (input != null) {
        input = input.toLowerCase();
        return input.substring(0, 1).toUpperCase() + input.substring(1);
      }
    }
  })
  .controller('NameInput', function($scope) {
    $scope.yourName = "";
  });
<body ng-app="todoApp" ng-cloak>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js"></script>
  <div id="wrapper">
    <div ng-controller="NameInput">
      <label>Name:</label>
      <input id="name-input" type="text" ng-model="yourName" placeholder="Enter a name here">
      <hr>
      <h1>Hello {{yourName | NameInput}}</h1>
    </div>
  </div>
</body>

Sign up to request clarification or add additional context in comments.

2 Comments

Thank bro, exactly what i was looking for!
Chill out!! Have fun.. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.