首页 > 解决方案 > 在角度控制器中使用文本角度函数?

问题描述

我是Angular的新手,正在尝试更改我网站后端上 textangular 文本输入字段的默认行为我是吉他手,不是网络开发人员,但通常可以弄清楚我需要做什么......)。该字段具有指定 youtube url 的选项,然后textangular将其转换为图像,以便在后端进行编辑。然后在前端textangular具有将 youtube url 正确显示为 iframe 的方法。

但是,在前端,我没有使用angular,在我的情况下我读到了,要让 youtube 嵌入以 iframe 的形式显示在前端,您必须使用taApplyCustomRenderers函数。

例如,请参见此处:

如何在范围 var 的 textAngular 编辑器中去除占位符 img?

在我的角度应用程序中,以下是一些相关行:

angular.module('dashboard')
.controller('EditController', ['$scope', '$http', '$location', '$routeParams', function ($scope, $http, $location, $routeParams) {

$scope.save = function () {
    $scope.busy = true;
    // I thoguht I could do this:
    $scope.somefield = taApplyCustomRenderers($scope.somefield);
    // then I would save to model
    });

我收到taApplyCustomRenderers未定义的错误。根据我读到的内容,我认为taApplyCustomRenderers在使用textangular时是一个可用的函数,但是对此我很陌生,我想我错过了一些关于将函数注入控制器或其他东西的关键步骤。

希望有人可以提供一些启示。

提前致谢!布赖恩

标签: angularjstextangular

解决方案


TLDR;当您尝试访问时,taApplyCustomRenderers您会收到一个错误,因为它没有提供给当前函数,注入该函数并且它将起作用。

问题

虽然我从未真正尝试过使用 textAngular,但让我解释一下问题所在,从那里应该很容易找到解决方案。

EditController只是一个常规的 javascript 函数,它运行并附加到相关的 DOM 元素,因此它只能访问在其自己的范围内(或全局)声明的函数。

这是您的确切代码,只是缩进不同,因此您可以更好地理解:

angular.module('dashboard').controller(
    'EditController',
    [
        '$scope',
        '$http',
        '$location',
        '$routeParams', 
        function ($scope, $http, $location, $routeParams) {
            ...
            $scope.somefield = taApplyCustomRenderers($scope.somefield);
        }
    ]
);

如您所见, thecontroller function有两个参数,第一个是 a string,第二个是 an array,最后element一个array是常规的function

解决方案

检查textAngular 文档我看到这taApplyCustomRenderers是一个工厂,这意味着您可以将它注入到您的控制器函数中,如下所示:

angular.module('dashboard').controller('EditController', 
    ['$scope', '$http', '$location', '$routeParams', 'taApplyCustomRenderers',
     function ($scope, $http, $location, $routeParams, taApplyCustomRenderers) { 
         taApplyCustomRenderers(); // is now Available.
     }
    ]);

推荐阅读