4

我正在编写一个指令以在名为的 HTML 表中显示来自服务器的数据djlist

directive('djlist', function(urls) {
    return {
        restrict: 'ACE',
        templateUrl: urls.list_objs_template,
        scope: {},
        controller: ['$scope', '$resource', 
            function($scope, $resource) {
                $scope.objs = $resource(urls.list_objs);
                $scope.objs_api = $resource(urls.list_objs_api);
                $scope.data = $scope.objs.get();
            }
        ]
    };
})

来自服务器的数据以 显示ng-repeat。数据数组中的每个对象都有一个附加的删除按钮,这是另一个名为djdel

<div class="row panel panel-primary">
    <h3 class="panel-heading">Data from REST</h3>
    <div class="panel-body">
        <table class="table">
            <tr>
                <th>Content</th>
                <th>Date Created</th>
                <th>Action</th>
            </tr>
            <tr ng-repeat="d in data.objects">
                <td>{{ d.content }}</td>
                <td>{{ d.date }}</td>
                <td>
                    <djdel ng-click="del($index)" model-pk="d.id"></djdel>
                </td>
            </tr>
        </table>
    </div>
</div>

这是我的定义djdel

directive('djdel', function() {
    return {
        restrict: 'ACE',
        template: '<button class="btn btn-danger btn-small">Delete</button>',
        scope: {
            modelPk: '='
        }, controller: ['$scope', '$http', '$resource', 
            function($scope, $http, $resource) {
                $scope.del = function(index) {
                    var $parent = $scope.$parent.$parent;
                    $parent.objs_api.
                    remove({id: $scope.modelPk}, function() {
                        $parent.data.objects.splice(index, 1);
                    });
                };
            }
        ]
    };
}).

这行得通。但是,在我的对象从服务器中成功删除后,djdel scopedjlist scope. 范围层次结构是djlist > ng-repeat > djdel,因此$scope.$parent.$parent在引用数据收集时。

有什么方法可以避免在范围链上引用这么多级别?

4

2 回答 2

6

您可以require父控制器: 在djdel

directive('djdel', function() {
    return {
        ...
        require: "^djlist",
        ...
        link: function(scope, elem, attrs, djlistController) {
            // the controller function does not have access to the required
            // controllers, so we just inject htem in the scope
            scope.djlistController = djlistController;
        },
        controller: ['$scope', '$http', '$resource', 
            function($scope, $http, $resource) {
                // you can access members of the djlistController as
                $scope.djlistController.XXX();
                ...
            }]
    };
});

djlist将所需的功能添加到(this不是$scope):

directive('djlist', function(urls) {
    ...
    controller: ['$scope', '$resource',
        function($scope, $resource) {
            this.XXX = function() {
                // you can also add variables here
            };
            ...
        }]
    ...
});
于 2013-11-12T10:53:15.857 回答
0

$rootScope您可以随时尝试在应用程序中发出事件。

$rootScope.$broadcast("nameOfTheEvent", paramsToSend);

在您从服务器中删除某些内容之后,

$rootScope.$on("nameOftheEvent", function(paramsToRecieve) { refreshList();});

当然,paramsToRecieve 是 paramsToSend,可以是任何对象。

于 2013-11-12T10:50:31.163 回答