Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have three buttons here wish to make $scope. -> q <- q as variable. How can I achieve that. This is just a test code my actual problem is based on making q as variable so please do not suggest any workaround

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.name = function(q){
    $scope.a = "John Doe "; // $scope.q how to make q varibale
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
Name: <button ng-click="name('a')" ng-model="name">this is a</button>
<button ng-click="name('b')" ng-model="name">this is b</button>
<button ng-click="name('c')" ng-model="name">this is c</button>
{{a}} via a<br>
{{b}} via b<br>
{{c}} via c<br>
</div>

share|improve this question
up vote 4 down vote accepted

Just add q as a key to scope because $scope in nature is an object

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.name = function(q){
    $scope[q] = "John Doe "; // $scope.q how to make q varibale
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
Name: <button ng-click="name('a')" ng-model="name">this is a</button>
<button ng-click="name('b')" ng-model="name">this is b</button>
<button ng-click="name('c')" ng-model="name">this is c</button>
{{a}} via a<br>
{{b}} via b<br>
{{c}} via c<br>
</div>

share|improve this answer
    
This is really helpful however there is this problem that after $scope.b is set why the value in $scope.a still exist ? how can i bypass that – sam Jan 31 at 9:24
    
Thats the nature of js variables value assigned retains it till memory – Vinod Louis Jan 31 at 9:27
    
is there no workaround for it not to happen? – sam Jan 31 at 9:33
    
The only way is to overwrite it – Vinod Louis Jan 31 at 9:36
    
$scope.name = function(q){ var ar = ["a","b","c"] for(var i=0;i<=ar.length;i++) { $scope[ar[i]]="" } $scope[q] = "John Doe "; } Is this the right way to do it? – sam 2 days ago

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.name = function(q){
    $scope[q] = "John Doe "+ q; // $scope.q how to make q varibale
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
Name: <button ng-click="name('a')" ng-model="name">this is a</button>
<button ng-click="name('b')" ng-model="name">this is b</button>
<button ng-click="name('c')" ng-model="name">this is c</button>
{{a}} via a<br>
{{b}} via b<br>
{{c}} via c<br>
</div>

share|improve this answer

Just add q as a key to $scope Beause $scope is also an object. try this code:-

Controller

var app = angular.module('myApp', []);
app.controller('ctrl', function($scope) {
    $scope.name = function(q){
    $scope[q] = "Sam"; // now $scope.q will be a varriable
    }
});

in html:-

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<div ng-app="myApp" ng-controller="ctrl">
Name: <button ng-click="name('a')" ng-model="name">this is a</button>
<button ng-click="name('b')" ng-model="name">this is b</button>
<button ng-click="name('c')" ng-model="name">this is c</button>
{{a}} via a<br>
{{b}} via b<br>
{{c}} via c<br>
</div>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.