Javascript 在一个angularjs控制器中访问本地函数
我对angularjs和javascript非常陌生。我有这样的工作代码: my_ctrl.jsJavascript 在一个angularjs控制器中访问本地函数,javascript,angularjs,Javascript,Angularjs,我对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
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();
});
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 getmymodel">
<td>{{model.name}}</td>
</tr>
</tbody>
</table>
</div>
<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>
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>
<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>
名称
{{model.name}
将其分配给变量,如
var vm = this;
内部控制器
并定义函数,如
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>
...
和调用函数
{{model.name}
...