angular.js - angularjs的ng-repeat指令内如果嵌套自定义指令,内容渲染错误,
漂亮男人
漂亮男人 2017-05-15 16:56:51
[AngularJS讨论组]

component_list.tpl

component.tpl

directive

// se-component-list
Base.directive('seComponentList', function() {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: STATIC_PATH + 'js/seditor/tpls/base.component_list.tpl',
        controller: ['$scope', 'InitBaseData', function($scope, InitBaseData) {
            InitBaseData.getComponentList().success(function(data) {
                if (data && data.code == 0) {
                    $scope.component_list = data.component_list;
                } else {
                    alert('网络异常,请稍后重试');
                }
            }).error(function(err) {
                alert('网络异常,请稍后重试');
            });
        }]
    };
});
// se-component
Base.directive('seComponent', function() {
    return {
        restrict: 'A',
        replace: true,
        templateUrl: STATIC_PATH + 'js/seditor/tpls/base.component.tpl',
        link: function(scope, element, attr) {
            element.bind('drop', function(event) {
                event.preventDefault();
                console.log('drop');
            });
        }
    }
});

问题描述,如果把component.tpl的内容直接替换,输出是正常的,但是我需要为component添加拖拽事件。目前的问题是,本来应该显示在其他boxes里面的内容都集中显示在最后一个boxes里面了,小弟刚学angular没多久,问题可能比较肤浅,但却是搜不到答案,还请讲解下,谢谢。

漂亮男人
漂亮男人

全部回复(2)
仅有的幸福

ng-repeat 是会创建自己的作用域的,子作用于访问 controller 的 $scope 需要
$parent.xxx
才行

建议你的seComponent 通过设置属性的方式来取内容

<p se-component list="component_type.list"></p>
//具体是不是这样写忘了...,需要验证下,就是这个意思
PHPz

发现把se-component的replace:true去掉,显示就正常了,但是会多了个<se-component>的标签,不好看

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号