1

I have created a custom directive. The controls defined in templates seem to be working fine. But, I need to append another control - image button, to a control defined in templates according to some condition. To do this I included an IF condition, created an image button and added a 'ng-click' attribute to it. However, using javascript 'append' of image button to my template control's ('html') does not seem to work as it says no 'append' is an undefined function. Hence I used 'concat()', this renders the image button but 'ng-click' does not work. The function in the controller does get hit. Please help!

 <div ng-controller="pdfFormsEditController">
                <split-pane id="splitPane" height="800">
			<split-pane-component id="leftPane" width="50%"> <div class="form-editor boxed-section dashed" id="form">
                        <form-control ng-repeat="control in formDefinition.Definition.controls" control="control" />
                    </div></split-pane-component>                                  
		</split-pane>
            </div>

This is my directive file -

"use strict";

angular.module('configurePDF.directives.noteForms', [])

.directive('formControl', ['$compile', function ($compile) {
    var templates = {
    
       "NoteLabel":'<label ng-click="onSelectNoteField(control.Name)" ng-style="control.style" style="font-style:Bold; font-size: 8.25pt; font-family:Arial">{{control.Properties.DisplayName}}</label>',
       "NoteMedcinFinding":'<p ng-click="onSelectNoteField(control.Name)" ng-style="control.style"><input  type="image" src="../Images/YesNo.gif" style="Width:35px; Height:17px;"><label style="font-style:Bold; font-size: 8.25pt; font-family:Arial">{{control.Properties.OriginalText}}</label></input></p>'     
     
    }

    var linker = function(scope, element, attrs) {
        
        var location = scope.control.Properties.Location.split(',');
        var size=scope.control.Properties.Size.split(',');  
       // var font=scope.control.Properties.Font.split(',');      
        
        scope.control.style = {position: "absolute", left: (parseInt(location[0])*1.5) + "px", top: location[1] + "px" ,minWidth:425+"px",height:size[1]+"px"};

        var html = templates[scope.control.Type];    
        debugger;

        if(scope.control.Properties.DetailFormID !=  "00000000-0000-0000-0000-000000000000"){
       
            var img = document.createElement('input');
            img.type = "image"
            img.src = "../../Images/plus.png";
            img.height = 10;
            img.width = 10;
            img.style.position = 'absolute'
            img.style.left =  ((parseInt(location[0])*1.5) - 13) + "px"
            img.style.top = (parseInt(location[1]) + 3)+ "px"
            //img.className ="ng-scope ng-binding"
            //img.onclick = "onSelectNoteField(scope.control.Properties.DetailFormID)"
            var attr = document.createAttribute("data-ng-click");
            attr.value = "onSelectNoteField(control.Properties.DetailFormID)";
            img.attributes.setNamedItem(attr);
            debugger;
            html = html.concat(img.outerHTML);
           //console.log(element);
        }
         var elem =  $compile(html)(scope);  
        element.replaceWith(elem);
    }

    return {
        restrict: "E",
        replace: true,
        link: linker,
        scope: {
            control:'='
        }
    };

}]);

UPDATE: I read that class="ng-scope ng-binding" should be created automatically in the element's html. But in my image button the class is is just "ng-scope". So, may be there is some binding issue to the scope, because I was not able to append the image button but concatenate it??

   <input type="image" src="../../Images/plus.png" height="10" width="10" style="position: absolute; left: 32px; top: 83px;" data-ng-click="onSelectNoteField(control.Properties.DetailFormID)" class="ng-scope">

Controller:

/
      .controller('pdfFormsEditController', ['$scope', '$routeParams', 'pdfFormsService','mastersService','$rootScope',
        function pdfFormsEditController($scope, $routeParams, pdfFormsService,mastersService,$rootScope) {
            var allNoteFields = [];
            $scope.editMode = true;
           
          
           $scope.onSelectNoteField = function(noteField) {  
            debugger;
             $scope.formDefinition = {};          
             var result = pdfFormsService.getFormDefinition('3151ff0d-6c93-4c80-9182-fd05f7d6cf90');
             
            result.success(function (formDefinition) {
            console.log(formDefinition);
            $scope.formDefinition = formDefinition;
            var size = $scope.formDefinition.Definition.Properties.Size.split(',');
            $scope.formDefinition.style = { position: 'relative', width: size[0] + "px", height: size[1] + "px" }

                
            }      

3
  • Have you tried to replace the element first, compile later? Commented Oct 1, 2014 at 11:47
  • Do you mean this? 'element.replaceWith(html); element = $compile(element)(scope); '. This does not create the 'class' attribute.. Commented Oct 1, 2014 at 11:56
  • Forget my previous comment - I only used $compile() in a different way so far. Commented Oct 1, 2014 at 12:56

1 Answer 1

0

Well, I found the answer and its pretty simple! My directive had no access/link to the scope when I was calling onSelectNoteField! Hence a different approach worked.

    if ((scope.control.Properties.DetailFormID != "00000000-0000-0000-0000-000000000000") && (scope.control.Properties.DetailFormID != "0") && (scope.control.Properties.DetailFormID !== undefined)) {

            var img = document.createElement('input');
            img.type = "image";
            img.src = "../../Images/plus.png";
            img.height = 10;
            img.width = 10;
            img.style.position = 'absolute'
            img.style.left = ((parseInt(location[0]) * 1.5) - 13) + "px"
            img.style.top = (parseInt(location[1]) + 3) + "px"
            //img.className ="ng-scope ng-binding"
            //img.onclick = "onSelectNoteField(scope.control.Properties.DetailFormID)"
            var attr = document.createAttribute("data-ng-click");
            attr.value = "onImageClick('" + scope.control.Properties.DetailFormID + "')";
            img.attributes.setNamedItem(attr);
            html = html.concat(img.outerHTML);

        }

        var elem1 = $compile(html)(scope);
        element.replaceWith(elem1);

        scope.onImageClick = function (subformID) {
            var id = subformID;
            scope.onImageClick = function (control) {
                var scope = angular.element($("#form")).scope();
                scope.onSelectNoteField(id);
            }
        }

I am creating a scope property- the function onImageClick and calling this in ng-click of my image. onImageClick gets the scope of my parent element, which has access to the function I want to call - onSelectNoteField!

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

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.