Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Directives
Message
Information générale
Forum:
Javascript
Catégorie:
Autre
Titre:
Divers
Thread ID:
01605023
Message ID:
01605030
Vues:
19
>>Hi everybody,
>>
>>I am still not clear how directives work and how to make them optional.
>
>The link function passes a third parameter which lets you get at the attributes:
>
>
>// Instead of this:
>            link: function (scope, element) {
>// Do this:
>            link: function (scope, element, attrib) {
>
>
>Now you have access to the attributes, so to grab the value:
>
>
>var attribValue = attrib.smFocus
>
>
>(take a look at attrib in the debugger and you'll see the values it holds).

Now instead of evaluating the "isNew" variable the attribute is now "isNew". What should I do? Here is my new code:
(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).
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform