0

hello i would like to know the steps of execution of directives and some fundamental details about how the processing happens this would be very helpful for more customization of directives and exploit its features...

i have a sample template with a directive and the attributes needed and few html tags as shown

template1:-

<directive1 s-web-service-path="object.WebServicePath" >

<h1>any html content</h1>

</directive1>

my directive is i.e. it calls a web service to get its content

widgetsModule.directive("directive1", function ($http) {
try {
    return {
        restrict: "E",
        replace: true,
        transclude: true,
        scope: {


            sWebServicePath: "="
        },

        template: '<div ng-transclude><h1>My content {{result }}</h1> </div> ',

        link: function (scope, element, attrs) {

            var request = '<request struct>';

            $http({ method: "POST", url: scope.sWebServicePath, data: request }).
            success(function (data, status) {
                scope.result = data;
            }).
            error(function (data, status) {
                alert("Service Call Error");
            })
        }
    }
}
catch (e) {
    alert(e);
}
});

What is the difference between attrs and $scope in the link function... in above case

 $scope.sWebServicePath gives me the value of object.WebServicePath i.e. something like     "http://anypath.asmx"

but

attrs.sWebServicePath  give me object.WebServicePath...  why is this difference and how is it useful?

I know that putting "ng-transclude" would enable me to have

<h1>any html content</h1>

in the specified div of my template with in the directive but how does the execution happens?

and why is that return is to be written in a directive it returns a link function alright and it can be used for DOM manipulation but any example of when do i use it? i know these might sound very fundamental but please do throw some light on the execution flow of the directive...or some good source of reference...thank you! and advice/tips on the usage of the parameters of the directive would be very helpful

2 Answers 2

1

this link has the best documentation on usage of directives they should have put this link under directive definition ... js came across this page while reading the angular documentation

http://docs.angularjs.org/api/ng.$compile

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

1 Comment

yea, this page of documentation is very usefull
0

if you use http request in directive you dont forget for $watch because http calls are asynchronous.

Diference between attr and scope is:

scope - is own scope of directive

attr - are attribs of directive

i think so data passed through attr not have to always same as your own scope, because you can make your new own scope for directive but cant change attr.

may be helpful for you

about-those-directives
egghead.io
onehungrymind.com/angularjs-directives-basics/
2013 - angularjs-directives-using-isolated-scope-with-attributes

many sources

AngularJS-Learning

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.