将javascript函数调用转换为typescript函数

时间:2018-05-01 21:58:04

标签: javascript angular typescript

我将一个较旧的角度较小的应用程序转换为角度5.现在我对打字稿非常新,而且没有在线找到太多帮助。我感到好奇的是在我的代码下面我使用服务来创建对话框。在那之内我通过以下方式调用函数:

tempModalDefaults.controller = function ($scope, $modalInstance) {

我想知道如何做到这一点。当.controller没有被定义时,在typescript中处理tempModalDefaults.controller还有一个等效的。另外我如何处理angular.extend实例?

富乐代码:

    function show(customModalDefaults, customModalOptions) {
        //Create temp objects to work with since we're in a singleton service
        var tempModalDefaults = {};
        var tempModalOptions = {};

        //Map angular-ui modal custom defaults to modal defaults defined in service
        angular.extend(tempModalDefaults, modalDefaults, customModalDefaults);

        //Map modal.html $scope custom properties to defaults defined in service
        angular.extend(tempModalOptions, modalOptions, customModalOptions);

        if (!tempModalDefaults.controller) {
            tempModalDefaults.controller = function ($scope, $modalInstance) {
                $scope.modalOptions = tempModalOptions;
                $scope.modalOptions.ok = function () {
                    $modalInstance.close(tempModalOptions.okResult);
                };
                $scope.modalOptions.close = function () {
                    $modalInstance.dismiss('cancel');
                };
                $scope.modalOptions.rename = function () {
                    $modalInstance.close(tempModalOptions.bodyText);
                };
            }
        }

        return $modal.open(tempModalDefaults).result;
    }

1 个答案:

答案 0 :(得分:1)

var tempModalDefaults = {};定义您的类型,或将其定义为any

var tempModalDefaults: any = {}
// or
var tempModelDefaults = {} as YourType
// or
var tempModelDefaults: YourType = {} as any 
// `as any` is needed because `{}` will not match `YourType`