简体   繁体   English

AngularJS - 我如何使用 $scope 在控制器中调用函数

[英]AngularJS - how can i call a function in a controller with $scope

I am trying to call a function inside my html button to convert an array into a string.我试图在我的 html 按钮中调用一个函数来将数组转换为字符串。 I just can't seem to call my function with this code.我似乎无法使用此代码调用我的函数。

html: html:

<button ng-click="arrayToString" ng-show="pl.ap>0">add to cart</button>

app.js:应用程序.js:

$scope.arrayToString = function(){
    for (i = 0; i < addedArray.length; i++){
        addedArrayString = addedArrayString + addedArray[i];
    };
};

On console it's throwing an error: "Uncaught SyntaxError: Unexpected token {"在控制台上,它抛出一个错误: "Uncaught SyntaxError: Unexpected token {"

错字代码.rename fuction ,以function

First of all, you have typo in the function keyword.首先,您在function关键字中有错字。

Also you have to call this function:你也必须调用这个函数:

ng-click="arrayToString()"

Change your code like this,像这样改变你的代码,

  $scope.arrayToString = function(){
    for (i = 0; i < addedArray.length; i++){
        addedArrayString = addedArrayString + addedArray[i];
    }
 };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在angularjs控制器范围内调用全局函数回调? - How would I call a global function callback in the scope of a angularjs controller? 如何使用$ scope方法在AngularJS控制器中调用函数? - How to call function in AngularJS controller with $scope method? 如何在AngularJS控制器中调用javascript函数? - How can I call javascript function in AngularJS controller? AngularJS:如何从控制器调用在指令范围内定义的函数? - AngularJS: How do I call a function defined in a directive's scope from a controller? 如何将$ scope中的值传递给AngularJS中的内部控制器? - How can I pass down a value in the $scope to an inner controller in AngularJS? 如何在AngularJS的另一个控制器中调用/使用$ scope的一个控制器 - How to call/use 1 controller of $scope in another controller in AngularJS AngularJS:从具有隔离范围的指令中调用控制器函数 - AngularJS : Call controller function from a directive with isolated scope 从html调用angularjs控制器中的函数,并且未定义范围变量 - call a function in angularjs controller from html and scope variable is not defined 我可以将$ scope变量从一个函数传递到控制器内的另一个函数吗? AngularJS - Can I pass a $scope variable from one function to another function inside the controller? AngularJS 如何从主干控制器执行 angularJS 控制器中的函数 - How can I execute a function in an angularJS controller from a backbone controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM