简体   繁体   中英

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. I just can't seem to call my function with this code.

html:

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

app.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 {"

错字代码.rename fuction ,以function

First of all, you have typo in the function keyword.

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];
    }
 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM