Angularjs directive adds directives as an attribute and binds them dynamically

Hi, I am working on a directive where I need to edit the DOM by adding the ng-src attribute and model to it.

This is my DOM

     <edit-component>
      <img src="images/logo.png" title="Hearty Wear" />
    </edit-component>

I need a DOM be result

       `<div>
         <img src="images/logo.png" title="Hearty Wear" ng-src={{myModel}} />
       </div> `

That way, when I update myModel with data, the image should be updated

UPDATE

sam.directive('editComponent', function() { return { restrict: 'EA', transclude: true, replace: true, templateUrl: "imageTemplate.html", link: function(scope, element, attb, ctrl, transclude) { scope.data = function() { var imagedata; imagedata = arguments[5]; scope.imageModel = imagedata.base64; return element.find('img').attr('src', "data:image/png;base64," + imagedata.base64); }; } }; });

I need the previous src attribute value to display an existing image.

I am currently updating the src attribute manually. I need a solution where I can update using the model variable

thank

0
2

AngularJS .

→ → preLink → postLink ()

angular (ng-model, ng-style, ng-src) <

var app;

app = angular.module('App', []);

app.directive('myDirective', [
  '$compile', function($compile) {  // Crucial Part
    return {
      scope: true,
      compile: function(element, attrs) {
        element.attr('ng-src', '{{imageModel}}');
        element.attr('ng-click', 'updateImage()');
        element.removeAttr('my-directive'); // Crucial Part
        return {
          pre: function(scope, ele, attb) {},
          post: function(scope, ele, attb) {
            $compile(ele)(scope);
            return scope.updateImage = function() {
              return scope.imageModel = "http://www.google.com/logos/doodles/2015/halet-cambels-99th-birthday-6544342839721984-hp2x.png";
            };
          }
        };
      },
      controller: function($scope, $element, $attrs) {
        return $scope.imageModel = null;
      }
    };
  }
]);
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <style>
    img {
      max-width: 100%;
      
    }
  </style>
</head>
<body ng-app='App'>
  <img src="https://www.google.co.in/images/srpr/logo11w.png" alt="" my-directive>

</body>
</html>
Hide result

, .

(): -

angular , ,

    element.attr('ng-src', '{{imageModel}}'); // For dynamic image url changes
    element.attr('ng-click', 'updateImage()'); // The trigger to update image
    element.removeAttr('my-directive'); // **Crucial step please remove your custom directive attribute**

(),

(): -

, ( , updateImage() postLink. !)

$scope.imageModel = null//

(): - - preLink, postLink. prelink.

postLink: - $compile (element) (scope). , , . (Vola). .

. , , .

JSBin https://jsbin.com/parote/edit?html,js,output

+4

<img ng-if="!myModel" src="images/logo.png" title="Hearty Wear"/>
<img ng-if="myModel" src="{{ myModel }}" title="Hearty Wear"/>
+1

All Articles