Доступ к локальным функциям в одном контроллере angularjs - PullRequest
0 голосов
/ 04 июня 2018

Я очень плохо знаком с 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>

Iхотел вызвать функцию js с помощью контроллера в HTML и сделать вызов функции локальным для контроллера.Я изменил код, как показано ниже, но не повезло.Любая помощь приветствуется!

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>

1 Ответ

0 голосов
/ 04 июня 2018

Назначьте это переменной как

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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...