javascript - In AngularJS, after clicking a button, how to retrieve one of the two Json files and put it in $scope variable? -
for example,
there 2 json files:
json file #1: [{colorname:blue},{colorname:blue}]
json file #2: [{colorname:red},{colorname:red}]
in controller, there variable $scope.color
in html, there 2 buttons, 1 called "colorblue". other 1 called "colorred".
what want is: when clicking button "colorblue", $scope.color
variable equals json file #1; when clicking button "colorred", $scope.color
variable equals json file #2.
thanks in advance!
you can bind function button click event. function called in $scope
:
function main($scope) { var file1 = [{ colorname: "blue" }, { colorname: "blue" }]; var file2 = [{ colorname: "red" }, { colorname: "red" }]; $scope.color = null; $scope.file = function(a) { switch (a) { case 1: $scope.color = file1; break; case 2: $scope.color = file2; break; } } }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app ng-controller="main"> <button ng-click="file(1)">colorblue</button> <button ng-click="file(2)">colorred</button> {{color}} </body>
Comments
Post a Comment