gpt4 book ai didi

javascript - 在一个 angularjs Controller 中访问本地函数

转载 作者:行者123 更新时间:2023-11-30 20:27:58 24 4
gpt4 key购买 nike

我对 angularjs 和 javascript 很陌生。我有这样的工作代码:

my_ctrl.js

app.controller('my_ctrl', function($scope, $filter, $http) {
$scope.getmymodel = function(){$http({
method: 'GET',
url: '/getmymodel',
headers: {'Content-Type': 'application/json'}
}).then(function(response){$scope.getmymodel=response.data;});};
$scope.getmymodel();
});

HTML:

<div ng-controller="my_ctrl as ctrl" ng-cloak>
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="model in getmymodel">
<td>{{model.name}}</td>
</tr>
</tbody>
</table>
</div>

我想在 HTML 中使用 Controller 调用 js 函数,并使函数调用在 Controller 本地进行。我修改了代码如下但没有运气。感谢您的帮助!

my_ctrl.js

app.controller('my_ctrl', function($scope, $filter, $http) {
this.getmymodel = function(){$http({
method: 'GET',
url: '/getmymodel',
headers: {'Content-Type': 'application/json'}
}).then(function(response){this.getmymodel=response.data;});};
this.getmymodel();
});

HTML:

<div ng-controller="my_ctrl as ctrl" ng-cloak>
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="model in ctrl.getmymodel">
<td>{{model.name}}</td>
</tr>
</tbody>
</table>
</div>

最佳答案

把这个赋值给变量,比如

var vm = this;

在 Controller 中

并像这样定义函数

vm.getmymodel = function(){};

在 HTML 中

<div ng-controller="my_ctrl as vm">
...
and call function

<tr ng-repeat="model in vm.getmymodel">
<td>{{model.name}}</td>
</tr>

...
</div>

关于javascript - 在一个 angularjs Controller 中访问本地函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50675313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com