Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.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
从ionic模板调用控制器中的javascript函数_Javascript_Angularjs_Ionic Framework - Fatal编程技术网

从ionic模板调用控制器中的javascript函数

从ionic模板调用控制器中的javascript函数,javascript,angularjs,ionic-framework,Javascript,Angularjs,Ionic Framework,创建我的第一个Ionic应用程序,到目前为止进展顺利。我不是一个用户界面的家伙,所以我肯定我没有用最好的风格做事,但无论如何 我有一个模板,其中包含此代码。(我已经拿出了额外的东西,并试图将其简化到问题的关键,因此忽略任何语法错误。) 我已经看过了《爱奥尼亚用户指南》,我要么错过了,要么就是做错了,错过了范例。我知道我可以在诸如ng Click=“myFunction()”等指令中调用绑定到$scope的JS函数,但是在显示某些内容时我该怎么做呢?我只想从模板中调用typeCodeToStrin

创建我的第一个Ionic应用程序,到目前为止进展顺利。我不是一个用户界面的家伙,所以我肯定我没有用最好的风格做事,但无论如何

我有一个模板,其中包含此代码。(我已经拿出了额外的东西,并试图将其简化到问题的关键,因此忽略任何语法错误。)

我已经看过了《爱奥尼亚用户指南》,我要么错过了,要么就是做错了,错过了范例。我知道我可以在诸如ng Click=“myFunction()”等指令中调用绑定到$scope的JS函数,但是在显示某些内容时我该怎么做呢?我只想从模板中调用typeCodeToString(entry.typeCode)


请注意,我不仅需要调用控制器中的函数,还需要传入模板中正在迭代的“条目”的类型代码。

我不知道typeCodeMappings如何准确返回字符串。但是,在控制器本身中定义函数可以实现您的要求。因为,您不希望从DOM而不是在控制器中调用函数

 app.controller('EntriesCtrl', function($scope, $http) {

       function typeCodeMappings(code){
             var string;
               if(code == "B"){
                  string = "Bike";
                  return string;
                }
              if(code == "A"){
                  string = "Auto";
                  return string;
                }
              if(code == "T"){
                  string = "Train";
                  return string;
                }
               if(code == "P"){
                  string = "Plane";
                  return string;
                }
      }

        $http.get("...queryUrl...").then(
            function(response) {
                $scope.entries = response.data.results;
//i dont know your entries structure but loop the conversion
 angular.forEach($scope.entries, function(value, key) {
               value.typeCode = typeCodeMappings(value.typeCode);});
            },
            function(reason) {
                console.log("Failed reading entries, reason=" + reason);
            }
        );
        }

我不知道typeCodeMappings是如何返回字符串的。但是,在控制器本身中定义函数可以实现您的要求。因为,您不希望从DOM而不是在控制器中调用函数

 app.controller('EntriesCtrl', function($scope, $http) {

       function typeCodeMappings(code){
             var string;
               if(code == "B"){
                  string = "Bike";
                  return string;
                }
              if(code == "A"){
                  string = "Auto";
                  return string;
                }
              if(code == "T"){
                  string = "Train";
                  return string;
                }
               if(code == "P"){
                  string = "Plane";
                  return string;
                }
      }

        $http.get("...queryUrl...").then(
            function(response) {
                $scope.entries = response.data.results;
//i dont know your entries structure but loop the conversion
 angular.forEach($scope.entries, function(value, key) {
               value.typeCode = typeCodeMappings(value.typeCode);});
            },
            function(reason) {
                console.log("Failed reading entries, reason=" + reason);
            }
        );
        }

您可以在$scope上指定typecodeMappings,如下所示

 $scope.typeCodeMappings = { 
"B" : "Bike", 
"A" : "Auto", 
"T" : "Train", 
"P" : "Plane" 
})

在html视图中

{{entry.typeCode}}-{{typeCodeMappings[entry.typeCode]}


希望这有帮助。

您可以在$scope上指定您的typecodeMappings,如下所示

 $scope.typeCodeMappings = { 
"B" : "Bike", 
"A" : "Auto", 
"T" : "Train", 
"P" : "Plane" 
})

在html视图中

{{entry.typeCode}}-{{typeCodeMappings[entry.typeCode]}


希望这有帮助。

需要显示相关的控制器代码和数据示例,以便我们知道您在谈论有关
typeString的内容。
您也可以发布控制器吗,因此,更容易理解您所说的内容。添加的相关控制器代码需要显示相关控制器代码和数据示例,以便我们了解您所说的关于
typeString
的内容。您是否也可以发布控制器,以便更容易理解您所谈论的内容。添加了相关的控制器代码arg。我发现了我的问题。我的ionic实例正在缓存控制器,但没有更新它,因此它似乎无法工作。当我关闭浏览器并再次尝试时,它只需调用{}中嵌入参数的函数即可工作,例如Type:{{typeCodeToString(entry.typeCode)}}。我以为您想在控制器本身中调用函数:)Arg。我发现了我的问题。我的ionic实例正在缓存控制器,但没有更新它,因此它似乎无法工作。当我关闭浏览器并再次尝试时,它只需调用{}中嵌入参数的函数即可工作,例如Type:{{typeCodeToString(entry.typeCode)}}。我以为您想在控制器本身中调用函数:)