0

JAVASCRIPT:

.directive('search', [function () {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            attrs.$set('value', 'Word...');
            console.log(attrs);
        }
    };
}]);

And the value attribute is added, but not displayed. I think it has something to do with ngModel attribute not allowing to change value, but I've seen somewhere in AngularJS docs, that setting value programmatically is possible, so can someone please show me the way how?

HTML:

<input type="text"
ng-model="query"
ng-init="inputClass='input-inactive'"
ng-class="inputClass"
ng-focus="inputClass='input-active'"
ng-blur="inputClass='input-inactive'"
ng-change="searchModelChange()"
search />

Edit: In the end, I want to get it doing something as simple as displaying something like 'Search items...' before it's focused, when focused, set it to '', and when blured, check if value is '' and set it back to 'Search items...'.

I know it's pretty simple, and I already got this working once with outside JS function, but I feel like it's not the best solution to involve something like "getElementById", I think it must be possible with AngularJS from link function, it's just that I don't know how...

Edit 2: Different from placeholder. Is there a way to solve this? Say, if I needed to achieve something different from what I described in the first edit?

4
  • what is your end game? Commented Jul 17, 2015 at 14:19
  • To me it looks like you actually want to set the placeholder-attribute and not the value attribute. I might be wrong about your intentions though Commented Jul 17, 2015 at 14:19
  • @IvanChepurin Isn't that what placeholder for? Commented Jul 17, 2015 at 14:31
  • @PSL yea.. it is... I guess. I just dont like how it's still there when you focus Commented Jul 17, 2015 at 14:35

3 Answers 3

1

Something like this?

.directive('search', [
  function() {
    return {
      restrict: 'A',
      require: 'ngModel',
      link: function(scope, element, attrs, ngModel) {
        var defText = attrs.search;

        ngModel.$render = function(val) {
          var text = (!val && val !== 0) ? defText : val;
          element.val(text);
        }

        element.on('focus', function() {
          if (!ngModel.$viewValue)
            element.val('');
        });

        element.on('blur', function() {
          if (!ngModel.$viewValue)
            element.val(defText);
        });

      }
    };
  }
]);

angular.module('app', []).directive('search', [
  function() {
    return {
      restrict: 'A',
      require: 'ngModel',
      link: function(scope, element, attrs, ngModel) {
        var defText = attrs.search;

        ngModel.$render = function(val) {
          var text = (!val && val !== 0) ? defText : val;
          element.val(text);
        }

        element.on('focus', function() {
          if (!ngModel.$viewValue)
            element.val('');
        });

        element.on('blur', function() {
          if (!ngModel.$viewValue)
            element.val(defText);
        });

      }
    };
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<div ng-app="app">
  <input type="text" ng-model="query" search="Word..." />{{query}}
</div>

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

Comments

0

The placeholder attribute specifies a short hint that describes the expected value of an input field (e.g. a sample value or a short description of the expected format).

As an example,

<input type="text" ng-model="searchUser" placeholder="Enter a name to search." class="form-control search-filter">

Are you looking for this?

1 Comment

placeholder could do it, but its behavior is a little different from what I described
0

You can use onfocus & onblur to change the placeholder:

<input 
  type="text" 
  ng-model="searchUser" 
  placeholder="Search for items" 
  onfocus="this.placeholder = ''" 
  onblur="this.placeholder = 'Search for items'"
  class="form-control search-filter"
 >

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.