angularjs - 在角度控制器中使用文本角度函数?
问题描述
我是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时是一个可用的函数,但是对此我很陌生,我想我错过了一些关于将函数注入控制器或其他东西的关键步骤。
希望有人可以提供一些启示。
提前致谢!布赖恩
解决方案
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.
}
]);
推荐阅读
- css - 在悬停时将文本转换为图像
- python - 一次遍历两个列表一个元素
- python - 单击按钮在 tkinter 中绘制 matplotlib 图
- c++ - 如何将 arduino 库代码重写为 matlab 脚本?
- javascript - 如何下载、重命名和保存用户输入生成的图像
- python - Python打开子进程时如何修复wmctrl无法打开显示
- mockito - 无法为控制器的方法编写测试规范
- angular - 在 Angular 6+ httpClient 中,哪个响应状态代码会出现 catchError?
- c - 当 ADC 为 12 位时,DMA 为何提供 16 位值?
- ethereum - 如何在 Solidity 中管理大循环?