简体   繁体   English

jQuery克隆和追加功能未按预期在AngularJS指令中运行

[英]jQuery clone and append function isn't working as expected in AngularJS directive

in my app I'm trying I'm building a shopping cart. 在我的应用程序中,我正在尝试构建购物车。 You can see the app in this codepen: http://codepen.io/summerfreeze/pen/VjqJYW . 您可以在以下Codepen中看到该应用程序: http ://codepen.io/summerfreeze/pen/VjqJYW。 It's almost working, but I'm struggling with the last part. 它几乎可以正常工作,但是我在最后一部分方面很挣扎。 I want the "ADD ORDER LINE" button to add another order lines under the existing one. 我希望“添加订单行”按钮在现有的订单行下添加另一条订单行。 I'm trying to do this using jQuery: 我正在尝试使用jQuery:

  myApp.directive('myDirective', function($scope) {
    $scope.addline = function() {
      $(".orderline").clone().appendTo('.main');
    };
    return addline();
  });

But this doesn't seem to work. 但这似乎不起作用。 I would be grateful if someone would look at the code and tell me what mistake did I make. 如果有人能看一下代码并告诉我我犯了什么错误,我将不胜感激。

Not sure why you were using a directive. 不知道为什么要使用指令。 I removed that from your code and it works. 我从您的代码中删除了它,并且可以正常工作。 You still have other errors, but I'm guessing you can attend to those. 您仍然还有其他错误,但是我想您可以解决这些问题。 Here's the link to the modified version 这是修改版本的链接

new codepen version 新的Codepen版本

   $scope.addline = function(){
    $(".orderline").clone().appendTo('.main');
  };

As the others suggested, in order to follow clean code standards, please refrain from using jQuery code in AngularJS, in time it will lead to problems. 正如其他人所建议的那样,为了遵循简洁的代码标准,请不要在AngularJS中使用jQuery代码,否则会导致问题。

据我所知,您不应该附加到#main ,而应该附加到#main div[0]

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

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