Non
Non

Reputation: 8589

Directive with a template issue. AngularJS

I am having a little issue trying to translate a pure JS to a directive I am working on:

first I had it this way:

var searchbox={
	topPos_open:0, topPos_close:-50, isOpen:false,
	open:function(){
	    var box=document.getElementById("searchbox");
	    box.style.top=this.topPos_open+"px";
        document.getElementById("searchfield").focus();
        this.isOpen=true;
	},
	close:function(){
	    var box=document.getElementById("searchbox");
	    box.style.top=this.topPos_close+"px";
	    this.isOpen=false;
	},
	pop:function(){
	    !this.isOpen?this.open():this.close();
	},
	clear:function(){
		document.getElementById("searchfield").value="";
	}
}
#searchbox{position:fixed; top:-50px; left:0px; width:100%; height:60px; background-color:rgba(135, 206, 235, 1); -webkit-transition:top 0.5s ease 0s; -moz-transition:top 0.5s ease 0s; transition:top 0.5s ease 0s;}
#searchbox input{border:0px none; margin:0px 10px 0px 10px; padding:0px; width:80%; font-size:20px;}
#searchbox #input{float:left; background-color:rgba(255, 255, 255, 1); border:1px solid #dddddd; border-radius:20px; margin:5px; padding:5px; width:70%; min-width:250px;}
#searchbox #close{float:right; padding:10px:}
#searchbox button{border:0px none; background-color:transparent; font-size:20px; cursor:pointer;}
#searchbox #dots{clear:both; text-align:center; font-size:25px; cursor:pointer;}
<div id="searchbox">
    <div id="input">
      <input type="text" id="searchfield" value="">
      <button type="button" onclick="searchbox.clear()">
        X
      </button>
    </div>
    <div id="close">
      <button type="button" onclick="searchbox.close()">
        Cancel
      </button></div>
    <div id="dots" onclick="searchbox.pop()">
      ......
    </div>
</div>


<br>
<br>
<br>
<br>
Click the dots 

now I am updating some functions and I am trying to convert that into a directive, so this is I have so far:

.directive('searchbox', function() {
  return {
    template: '<div id="searchbox"><div id="input"><input type="search" id="searchfield" value=""></div></div>',
    restrict: 'E',
    link: function(element, scope) {
      var searchbox = {
          topPosOpen: 0,
          topPosClose: -70,
          isOpen: false,
          open:function() {
            var box = document.getElementById('searchbox');
            box.style.top = this.topPosOpen + 'px';
            document.getElementById('searchfield').focus();
            this.isOpen = true;
          },
          close:function() {
            var box = document.getElementById('searchbox');
            box.style.top = this.topPosClose + 'px';
            this.isOpen = false;
          },
          pop:function() {
            !this.isOpen ? this.open() : this.close();
          },
          clear:function() {
            document.getElementById('searchfield').value = '';
          }
        }
    }
  }
});

html:

<searchbox></searchbox>

but it is not working at all, any suggestions ?

Upvotes: 0

Views: 37

Answers (1)

thedoctor
thedoctor

Reputation: 1508

You need to define the additional methods on the controller as follows:

.directive('searchbox', function() {
  return {
    template: '<div id="searchbox"><div id="input"><input type="search" id="searchfield" value=""></div></div>',       
    restrict: 'E',
    scope: {},
    controller: function ($scope) {
      $scope.topPosOpen=0;
      $scope.topPosClose=-70;
      $scope.isOpen = false;
      $scope.open = function() {
            var box = document.getElementById('searchbox');
            box.style.top = $scope.topPosOpen + 'px';
            document.getElementById('searchfield').focus();
            $scope.isOpen = true;
          };

      $scope.close = function() {
            var box = document.getElementById('searchbox');
            box.style.top = $scope.topPosClose + 'px';
            $scope.isOpen = false;
          };

          $scope.pop = function() {
            !$scope.isOpen ? $scope.open() : $scope.close();
          };

          $scope.clear = function() {
            document.getElementById('searchfield').value = '';
          }
    }
  }
});

Upvotes: 1

Related Questions