0

I created a page with dynamic content in pure javascript and angular in similar.

I used apply method in javascript to reuse the function assigned to other element. It works perfectly. I am unable to do the same in angular. please see the below plunkers created in JS and Angular. In function move() method, by onclick I animated to move the text to some distance. To see animation Click on all texts in output in both plunkers. In JS plunker To animate the firstChild in onload, in line 38 I used move.apply(firstElem); But I am unable to do the same thing in angular. I want to do something simlpy like I did in JS. Which is the right way to do? can we use $scope.$apply and solve this? or any other way? please help.

functionName.apply(elem); // javascript
$scope.move(elem); // angular

Using JavaScript

Using AngularJS

jake
  • 136
  • 1
  • 12

1 Answers1

0

You're looking at the angular part the wrong way. In normal Javascript/jQuery you generally code in an imperative way, as in you tell an element to "move 100px to the right" or whatever. So in your pure JS example, you tell the element to change its position and color.

In Angular, however, you want to code in a declarative way. In your case, this means that when you have all these text blocks, they should have an attribute to represent whether the element has moved to the right, e.g. if it should get the styles associated to a moved element.

Instead of modifying the element's style directly, we'll create a css selector to represent a moved element:

.moved {
    left: 100px;
    background: red;
}

We'll then use the ng-class directive to specify when an element should get this class; that is, when it has the moved attribute. We'll also change up the way $scope.move function is used, and remove the ng-init since it's completely unnecessary.

<div ng-class="{moved: x.moved}" ng-click="move(x)" class="divText" ng-repeat="x in myData">
    <div>{{ x.text }}</div>
</div>

Then for the JS part. With the ng-class, basically all the $scope.move function has to do is to give the text object a moved attribute:

$scope.move = function (text) {
    text.moved = true;
};

Then we'll use $timeout to cause the first element to move shortly after the data has been loaded:

  $timeout(function() {
      $scope.move($scope.myData[0])
  }, 500)

And that's it! You now have the same functionality in your Angular example as in your JS one. Here's a plunker link to the edited Angular example.

I'd recommend you to read this excellent stackoverflow answer regarding the differences of coding in jQuery vs Angular: "Thinking in AngularJS" if I have a jQuery background?

Community
  • 1
  • 1
Fissio
  • 3,748
  • 16
  • 31
  • Thank you for the lengthy explanation. But I have some problem here. Actually I want to send many style parameters in a function and move, scale or whatever dynamically, but not through css class. I want to resize the object by dynamically like (body.clientWidth - screen.width/2). It appears different sizes in different resolutions. I don't want to add 'px' or '%' using css class. please see the new plunker I did in angular. I added new function resize(param1, param2,....); I have edited the pure JS plunker to show you exactly what i wanted in angular too. please look into JS plunker. – jake Feb 23 '17 at 09:50
  • [JS DEMO](https://plnkr.co/edit/6UwKYopzrBLYtKV1YjRn?p=preview) ... And Here is my new angular plunker which I tried in line 12,13. [ANGULAR DEMO](https://plnkr.co/edit/updBWxAfK7ODmOAeMPUf?p=preview) Also added a new button(movedown). Even this function also applies to firstElement which moved onload. I want this to be worked in Angular like how it works in JS. thats the reason I gave variable 'thumb = this' in js. But how can I assign 'this' to currentTarget in angular. please help me to I solve this? – jake Feb 23 '17 at 09:50
  • If you have multiple dynamic styles you want to add, then just use [ng-style.](https://docs.angularjs.org/api/ng/directive/ngStyle) – Fissio Feb 23 '17 at 09:55
  • Thanks a lot Fissio for your time, I will look into that and try to work out with that. – jake Feb 23 '17 at 10:51