javascript - Accessing AngularJS constants -
i have next constant in angularjs app , want access values using function shown below. problem pass key string shown in code below returns undefined, wondering there way can convert string passed constant key in order homecoming right constant value corresponding key beingness passed? thanks
myapp.constant('clients', { clientdata: "clientdetails", clientlist: "clients" }); getconstv : function(key){ //this 1 returns clientdetails console.log(clients.clientdata); //this 1 fail...returns undefined console.log(clients.key); } how phone call getconste follows:
getconstv('clientdetails');
your problem not angularjs problem, javascript issue: in javascript can access associative arrays in 2 ways: 1 dotted notation array.key , other bracket notation array[key]. if utilize dotted notation, javascript seek access property name key within associative array. in object, though, there isn't attribute name , obtain undefined. on contrary, bracket notation lets decide how access associative array (with constant or variable). recap: notation
array["key"] produces same result
array.key but if want flexible solution (using variable) must utilize bracket notation way
array[key] where key is, clearly, variable.
difference between using bracket (`[]`) , dot (`.`) notation
javascript angularjs associative-array
No comments:
Post a Comment