0

I am trying to bind the id property value for a div using angularjs directive

I want a div container where id of the container will be passed as the parameter from the directive

<!DOCTYPE html>
<html lang="en"  ng-app="directivesModule">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>


<h3>zendynamix Map Directive</h3>
<zd-map map-id="indexmap" ></zd-map>


<script src="scripts/angular.js"></script>
<script>

    (function() {

        var zdMap =  function() {
            var template = '<div id="{{scope.mapId}}" > abd</div>'
            function link(scope, elem, attrs) {
                console.log("**********************"+scope.mapId)

            }
            return {
                scope: {
                    mapId:'@'

                },
                link: link,
                template: template
            };
        };




        angular.module('directivesModule', [])
                .directive('zdMap', zdMap);

    }());

</script>


</body>
</html>

but when i see inspect the element in bowser i am getting empty id value

enter image description here

please say how to go about it i need to bind the value of directive parameter to the template

dhana lakshmi
  • 847
  • 1
  • 12
  • 29

2 Answers2

0

You should not use the scope in the template outside of link function.

    (function() {

        var zdMap =  function() {
            var template = '<div id="{{mapId}}" > abd</div>'
            function link(scope, elem, attrs) {
                console.log("**********************"+scope.mapId)

            }
            return {
                scope: {
                    mapId:'@'

                },
                link: link,
                template: template
            };
        };




        angular.module('directivesModule', [])
                .directive('zdMap', zdMap);

    }());
<!DOCTYPE html>
<html lang="en"  ng-app="directivesModule">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>


<h3>zendynamix Map Directive</h3>
<zd-map map-id="indexmap" ></zd-map>




</body>
</html>

Please run the above snippet and check the id

Sravan
  • 18,467
  • 3
  • 30
  • 54
0

Since you're using angular 1.5+ might I suggest using .component rather than .directive

  (function() {

        var zdMap =  function() {
            var template = '<div id="{{$ctrl.mapId}}" > abd</div>'
            function controller() {console.log(this.mapId)}
            return {
                bindings: { // HERE !!
                    mapId:'@'

                },
                controller: controller
                template: template
            };
        };




        angular.module('componentModule', [])
                .component('zdMap', zdMap); // here!!!!

    }());

HTML:

<!DOCTYPE html>
<html lang="en" ng-app="componentModule">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>
<h3>zendynamix Map Directive</h3>
<zd-map map-id="indexmap" ></zd-map>
</body>
</html>
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233