Why is it that if I have the following directive it works:
angular.module('app', [])
.directive('selectOnFocus', function(){
return{
restrict: 'A',
link: function(scope, element){
element[0].focus();
}
};
});
But if I switch it to this, it says element.focus() is not a function. To get this code to work, I need to import jquery in my index.html.
angular.module('app', [])
.directive('selectOnFocus', function(){
return{
restrict: 'A',
link: function(scope, element){
element.focus();
}
};
});
Here is my HTML:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="app">
<div>
<label>Name:</label>
<input type="text" ng-model="yourName" placeholder="Enter a name here" select-on-focus >
<hr>
<h1>Hello {{yourName}}!</h1>
</div>
</body>
</html>