首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何以角度将ng-attr-id转换为id

如何以角度将ng-attr-id转换为id
EN

Stack Overflow用户
提问于 2015-05-29 21:19:32
回答 1查看 6.4K关注 0票数 1

我从下面的链接中使用了角JS中的拖放库,它的工作非常完美,因为它在div中没有id。

https://jasonturim.wordpress.com/2013/09/01/angularjs-drag-and-drop/

我正在使用HTML5拖放功能,但是我面临的问题是通过角度J生成每个元素的动态id,我尝试了ng-attr-id和id,但是我无法通过angular.element(element l).a t tr("id")获取元素的id

代码语言:javascript
复制
    Working Code
    ------------
    HTML:

        <div ng-controller="myCtl" style="margin-top :50 p x;">
                    <div class="row">
                        <div class="col-m d-1 col-m d-offset-1">
                            <p>Click and drag a color onto the grid to the right</p>
                            <div class="peg green" x-l v l-drag gable="true" data-color="green">Green</div>
                            <div class="peg red" x-l v l-drag gable="true" data-color="red">Red</div>
                            <div class="peg blue" x-l v l-drag gable="true" data-color="blue">Blue</div>
                            <div class="peg black" x-l v l-drag gable="true" data-color="black">Black</div>
                            <div class="peg grey" x-l v l-drag gable="true" data-color="grey">Grey</div>
                        </div>

                        <div class="col-m d-10">
                            <div n g-repeat="r in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]">
                                <span class="slot circle" ng-repeat="c in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" x-l v l-drop-target="true" x-on-drop="dropped(drag E l, drop E l)"></span>
                            </div>
                        </div>
                    </div>
                </div>

 Controller( between the modal and view for drag and drop):

angular.module('my App', ['l v l. directives .drag drop'])
  .controller('myCtl', ['$scope', function($scope) {
    $scope.dropped = function(dragEl, dropEl) {
      // this is your application logic, do whatever makes sense
      var drag = angular.element(dragEl);
      var drop = angular.element(dropEl);

      console.log("The element " + drag.attr('id') + " has been dropped on " + drop.attr("id") + "!");
    };
  }]);   

    Modal(fetch the id of the dragged object ):

        angular.element(element).at tr("id");

    Not Working Code
    ----------------

    HTML:

        <div ng-controller="dd Controller" style="margin-top :50 p x;">
            <div class="row">
                <div class="col-m d-1 col-m d-offset-1">
                    <p>Click and drag a color onto the grid to the right</p>
                    <div ng-repeat="list in List">
                        <div id={{list.Name}} class="item" x-lvl-drag gable="true">{{list.Name}}</div>
                    </div>
                </div>
                <div class="col-m d-10">
                    <div n g-repeat="r in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]">
                        <span class="slot circle" ng-repeat="c in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" x-l v l-drop-target="true" x-on-drop="dropped(drag E l, drop E l)"></span>
                    </div>
                </div>
            </div>
        </div>

    Controller( between the modal and view for drag and drop):
    angular.module('my App', ['l v l. directives .drag drop'])
      .controller('my C t l', ['$scope', function($scope) {
        $scope.dropped = function(drag E l, drop E l) {
          // this is your application logic, do whatever makes sense
          var drag = angular.element(drag E l);
          var drop = angular.element(drop E l);

          console.log("The element " + drag. at tr('id') + " has been dropped on " + drop .at tr("id") + "!");
        };
      }]);   

    Modal(fetch the id of the dragged object ):

        angular.element(element).at tr("id");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-30 04:00:39

访问“lvl-dra-drop.js”文件中的属性是不正确的,如果使用的是指令,则可以访问第三个参数中的属性。

链接:功能(范围,el,scope,控制器)

请参阅本例中的注释:

代码语言:javascript
复制
module.directive('lvlDraggable', ['$rootScope', 'uuid', function($rootScope, uuid) {
    return {
        restrict: 'A',
        link: function(scope, el, attrs, controller) {

            //angular.element is no longer necessary if it refers to the element.
            el.attr("draggable", "true");

            //Incorrect
            var id = angular.element(el).attr("id");
            //This is correct
            var id = attrs.id;

            if (!id) {
                id = uuid.new();
                el.attr("id", id); //Also here
            }

            //.. more ...
        }
    }
}]);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30539234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档