javascript - AngularJS - 添加功能
问题描述
我刚刚开始使用 AngularJS。我来自后端开发,所以这个 JavaScript 让我感到困惑。我遵循了 AngularJS 教程,并得到了基础知识。
我有一个“record-list.component.js”文件(名称来自 AngularJS 演示),它具有下载数据的功能:
angular.module('recordList').component('recordList', {
templateUrl: 'record-list/record-list.template.html',
controller: function RecordListController($http) {
var self = this;
// self.orderProp = 'age';
$http.get('http://X/camera/record/1/').then(function(response) {
self.recordings = response.data;
});
}
})
这一切都很好。但是,我想添加另一个调用 URL 的函数,以便后端可以执行一些魔术。所以我尝试了这样的事情:
angular.module('recordList').component('recordList', {
templateUrl: 'record-list/record-list.template.html',
controller: function RecordListController($http) {
var self = this;
// self.orderProp = 'age';
$http.get('http://blackvue.tozz.nl/camera/record/1/').then(function(response) {
self.recordings = response.data;
});
}
function DownloadFileController($file) {
$window.alert("Hi: " + $file);
}
})
这不起作用。我尝试了多种,例如控制器:功能,但似乎没有任何效果。有人可以指出我的好方向吗?
解决方案
@Luiz Carlos 他的回答是正确的!我让它使用以下代码:
angular.module('recordList').component('recordList', {
templateUrl: 'record-list/record-list.template.html',
controller: function RecordListController($http) {
var self = this;
// self.orderProp = 'age';
$http.get('http://X/camera/record/1/').then(function(response) {
self.recordings = response.data;
});
this.DownloadFileController = function ($file) {
window.alert("Hi!");
}
}
})
在 HTML 中我可以做到:
<button ng-click="$ctrl.DownloadFileController()">Test</button>
谢谢!
推荐阅读
- excel - 计算逗号分隔列表中的奇数和偶数
- neural-network - Pytorch minibatching 使模型无法训练
- ruby-on-rails - 如何在同一台服务器上部署单独的 Rails API 后端和 React 前端
- javascript - 如何使用 this 关键字向构造函数添加新属性?
- gcc - 如何编译 VBScript 以包含 DLL 处理程序?
- php - 如何在 Laravel 中使用 select 获取相关字段值?
- python-3.x - 如何更正输入提示中大括号的显示?
- mysql - MySql如何根据查询类型创建一行并添加子表?
- dataset - SSRS textrun 在外部聚合中使用第一个聚合(不同的数据集)
- angular - Angular:路由器导航不返回根网址