0

我有一个在我的模块中注册的控制器。但是,我想做的是访问 Angular 之外的 $scope,以便我可以更改模型。例如,我想做以下事情:

var sentboxCtrl = function ($scope){

    $scope.sent = Sent;
    $scope.deleteIcon = deleteIconSource;
    $scope.flagIcon = flagIconSource;
    $scope.attachmentIcon = attachmentIconSource;

    for(var i = 0; i < $scope.sent.length ; i ++)
    {
        $scope.sent[i]['checked'] = false;
    }

    $scope.delete = function(element)
    {
        for(var i = 0; i < $scope.sent.length; i ++)
        {
            if($scope.sent[i].id == element.id)
            {
                $scope.sent.splice(i,1);
            }
        }

    }

    $scope.deleteChecked = function()
    {
        for(var i = 0; i < $scope.sent.length; i++)
        {
            if($scope.sent[i].checked)
            {
                $scope.sent.splice(i,1);

                // We must account for sent.length changing
                // after splicing so we need to decrement to
                // account for that.
                i --;
            }
        }
    }

    $scope.getModel = function()
    {
        return $scope;
    }

};

我使用 sendboxCtrl.getModel 返回范围,然后我可以在正常上下文之外操作范围的属性。

我怎样才能做到这一点?

4

1 回答 1

0

您可以在 angular 的上下文之外定义一个对象,然后在控制器函数内部,将其值分配给 $scope:

var controllerScope = null;
var sentboxCtrl = function ($scope){
    controllerScope = $scope;
    ...
}

现在您可以访问 $scope 对象的所有属性。

于 2013-10-16T00:10:15.847 回答