使用嵌套控制器调用函数

calling a function using nested controllers

本文关键字:调用 函数 控制器 嵌套      更新时间:2023-09-26

被嵌套控制器击中。此代码的主要目标是使从.json超链接中提取的数据指向另一个文件/函数。

.html文件:

<div ng-app="myApp"  ng-controller="GetCtrl"  >
<li  ng-controller="ChannelCtrl" ng-repeat="x in Operator">
        <a  href="getChannels({{x.OP_Name}})" > {{ x.OP_Name}} <br/></a>
    </li></div>`

app.js:

app.controller('GetCtrl', function($scope, $http) {
    $http.get('partials/operator.json')
        .then(function (response) {$scope.Operator = response.data.Operator;});
});
app.service('ChannelList', function () {
    this.getChannels = function(chname) {
        document.write("In Idea");
    }
});
app.controller('ChannelCtrl',function($scope,$http,ChannelList) {
    ChannelList.getChannels();
});

.json文件:

 {
      "Operator" : [
           {"OP_Name":"IDEA"},
           {"OP_Name":"VODAFONE"},
           {"OP_Name":"AIRTEL"},
           {"OP_Name":"BSNL"},
           {"OP_Name":"DOCOMO"},
           {"OP_Name":"AIRCEL"},
           {"OP_Name":"DIALOG"}
      ]
 }

我面临的问题是,当时只有其中一个控制器在运行。我不能同时使用这两个控制器,因为我的输出不完整。我能够调用函数以及显示.json文件的内容。但我不能同时执行这两个动作。

输出必须像:

 .Idea
 .Airtel
 .
 .
 .

当我点击其中一个超链接时,必须调用getchannels()函数。

要调用控制器函数,您可能需要使用ng-click,并且不能使用href调用控制器函数。

<a href="#" ng-click="getChannels(x.OP_Name)"> {{ x.OP_Name}}</a><br/>

请看一下这个plunker