グローバルとしたいので、スクリプト全体で[color]
と[shape]
を使用できます。私はそれぞれを独立して更新する必要がありますが、私がサイトに追加し続けると、私は両方を一緒に使う必要があります。 Live previewAngularJS変数を渡す適切な方法
- 例は動作しません:
$scope.shapeSelected = response.data[color][shape];
- 例は、作業を行います。あなたはあなたの関数に引数を渡す必要はありません
$scope.shapeSelected = response.data.blue[shape];
var app = angular.module("computer", ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/main', {
controller: 'MainCtrl'
}).
otherwise({
redirectTo: '/main'
})
}])
.controller('MainCtrl', ['$scope', '$http', function($scope, $http) {
$scope.colorType = function(color) {
$http.get('stuff.json').then(function(response) {
$scope.colorSelected = response.data.type[color];
});
}
$scope.shapeType = function(shape) {
$http.get('shapes.json').then(function(response) {
$scope.shapeSelected = response.data[color][shape]; // <--- [color] is not getting pulled in on this function.
var resultsColorShape = $scope.shapeSelected; // <--- I would like to be able to store this incase i need it later.
console.log('resultsColorShape');
});
}
}]);
応じて要素の値にアクセスする方法です。 –
[ここにいる](https://plnkr.co/edit/GpadlgmF5uF0EGSA7HBk?p=preview) –