butterandfly / project_9

Blog & Remark
0 stars 0 forks source link

ng-repeat中使用transclude的scope的问题 #157

Open butterandfly opened 10 years ago

butterandfly commented 10 years ago

使用ng-transclude会令transclude的内容的作用域在外面,不能到达ng-repeat时的每一项。解决方法是编写自己的transclude,使$transclude使用ng-repeat的动态scope:

        link: function($scope, $element, $attrs, controller, $transclude) {
            if (!$transclude) {
                throw minErr('rgMultiTransclude')('orphan',
                    'Illegal use of rgMultiTransclude directive in the template! ' +
                        'No parent directive that requires a transclusion found. ' +
                        'Element: {0}',
                    startingTag($element));
            }

            var name = $attrs.rgMultiTransclude;

                        // 就是这里,把当前scope作为第一个参数传入到$transclude中
            $transclude($scope, function(clone) {
                var s = $scope;
                var jqElm = clone.filter('[rg-multi-transclude-name=' + name + ']');

                $element.empty();
                $element.append(jqElm);
            });
        }