简体   繁体   English

我们如何在javascript等角度应用$ scope。$ apply方法,例如function.apply(elem)?

[英]How can we use $scope.$apply method in angular like javascript apply method like function.apply(elem)?

I created a page with dynamic content in pure javascript and angular in similar. 我创建了一个页面,其中的动态内容使用纯JavaScript,相似的角度显示。

I used apply method in javascript to reuse the function assigned to other element. 我在javascript中使用了apply方法来重用分配给其他元素的函数。 It works perfectly. 它运作完美。 I am unable to do the same in angular. 我无法做到同样的角度。 please see the below plunkers created in JS and Angular. 请参阅以下在JS和Angular中创建的插件。 In function move() method, by onclick I animated to move the text to some distance. 在函数move()方法中,通过onclick,我可以将文本移动到一定距离。 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); 在JS plunker中,为onload上的firstChild设置动画,在第38行中,我使用了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. 我想像在JS中一样做一些简单的事情。 Which is the right way to do? 哪个是正确的方法? can we use $scope.$apply and solve this? 我们可以使用$ scope。$ apply解决此问题吗? or any other way? 或其他方式? please help. 请帮忙。

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

Using JavaScript 使用JavaScript

Using AngularJS 使用AngularJS

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. 在普通的Javascript / jQuery中,通常以命令式方式进行编码,例如,您告诉元素“向右移动100px”或其他。 So in your pure JS example, you tell the element to change its position and color. 因此,在您的纯JS示例中,您告诉元素更改其位置和颜色。

In Angular, however, you want to code in a declarative way. 但是,在Angular中,您希望以声明的方式进行编码。 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, eg 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: 我们将直接创建一个css选择器来表示移动的元素,而不是直接修改元素的样式:

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

We'll then use the ng-class directive to specify when an element should get this class; 然后,我们将使用ng-class指令来指定元素何时应获得此类。 that is, when it has the moved attribute. 也就是说,当它具有moved属性时。 We'll also change up the way $scope.move function is used, and remove the ng-init since it's completely unnecessary. 我们还将改变$scope.move函数的使用方式,并删除ng-init,因为它是完全不必要的。

<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. 然后是JS部分。 With the ng-class, basically all the $scope.move function has to do is to give the text object a moved attribute: 对于ng-class,基本上$scope.move函数要做的就是为text对象提供moved属性:

$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导致第一个元素在数据加载后不久移动:

  $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. 现在,您的Angular示例中具有与JS中相同的功能。 Here's a plunker link to the edited Angular example. 这是指向已编辑的Angular示例的链接。

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? 我建议您阅读有关jQuery与Angular编码差异的出色stackoverflow答案: 如果我有jQuery背景,则“在AngularJS中思考”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM