首页 > 解决方案 > 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);
}
})

这不起作用。我尝试了多种,例如控制器:功能,但似乎没有任何效果。有人可以指出我的好方向吗?

标签: javascriptangularjs

解决方案


@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>

谢谢!


推荐阅读