>// Instead of this: > link: function (scope, element) { >// Do this: > link: function (scope, element, attrib) { >>
>var attribValue = attrib.smFocus >>
(function () { 'use strict'; var app = angular.module('sysMgrApp'); app.directive('smFocus', [ '$timeout', function ($timeout) { return { restrict: 'A', link: function (scope, element, attribute) { var attribValue = attribute.smFocus; console.log('Element Id= ' + element[0].attributes["id"].value + ' attribValue=' + attribValue); if (attribValue=='' || attribValue == true) { scope.$on('sm:focus', function () { $timeout(function () { element[0].focus(); }, 10); }); } } }; }]); })();and this is the element's markup in the form:
<div class="form-group"> <div class="control-label col-lg-3"> <label title="Nickname">Nickname:</label> </div> <div class="controls"> <div class="col-lg-9"> <input type="text" name="nickname" id="nickname" ng-model="currentDynamicRule.nickname" class="form-control" ng-disabled="!isNew" placeholder="Nickname" required ng-maxlength="10" data-sm:focus="isNew" /> <div class="field-validation-error"> <span ng-show="form.editDynamicRule.nickName.$error.required && form.editDynamicRule.nickName.$dirty">Nickname is required.</span> <span ng-show="form.editDynamicRule.nickName.$error.maxlength && form.editDynamicRule.nickName.$dirty">Nickname cannot exceed 10 characters.</span> </div> </div> </div> </div>I see the attribute now is "isNew" instead of false (the value of isNew variable).