Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在一个angularjs控制器中访问本地函数_Javascript_Angularjs - Fatal编程技术网

Javascript 在一个angularjs控制器中访问本地函数

Javascript 在一个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

我对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();
});
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}
...