8

This is my controller code :

var selectableFields = ["customerName", "customerAddress"];

angular.forEach(selectableFields, function(field, key) {
var value = $.jStorage.get(field); //using jStore to get the values
$scope.field=value;

});

The objective is to be able to access {{customerName}} and {{customerAddress}} in the view. Can anyone please tell me what is the correct way of doing this?

0

2 Answers 2

28

Use $scope[field] = value; instead.

With $scope.field, you're creating a new attribute named field.

Sign up to request clarification or add additional context in comments.

3 Comments

+1, I would also recommend to the OP that he/she not add 'dynamic variables' directly to the scope object. Instead, if it is a form (for example), have it $scope.form[field] = value;.
I am facing same issue but not able to solve, can you help me?
Hi @sp00m, in my angularjs app I have created a config file where I am defining all constants. Its a CRUD functionality code with API's In my AngularJs application. I have an identifier for ex. response.userid now for different instance it could be response.productid And I want to create a constant something like (var id = 'productid'; ) in my config file where I can change it at top and it will check for relevant variable accordingly. for example if(response.id) then do this; // here What I should be doing in this case?
1
   var pages=["kedi","köpek","tavşan","ayi"]

pages.map(page =>{
        $rootScope["adChangeUrl"+page]="dene.com"
        $rootScope["imgSrc"+page]="images/deneme.png"
        $rootScope["imgAdTitle"+page]="deneme.com"
        $rootScope["imgAdAlt"+page]="https://deneme.com/"
    }
    )
//console.log Output

adChangeUrlayi: "dene.com"
adChangeUrlkedi: "dene.com"
adChangeUrlköpek: "dene.com"
adChangeUrltavşan: "dene.com"
imgAdAltayi: "https://deneme1.com/"
imgAdAltkedi: "https://deneme1.com/"
imgAdAltköpek: "https://deneme1.com/"
imgAdAlttavşan: "https://deneme1.com/"
imgAdTitleayi: "deneme.com"
imgAdTitlekedi: "deneme.com"
imgAdTitleköpek: "deneme.com"
imgAdTitletavşan: "deneme.com"
imgSrcayi: "images/deneme.png"
imgSrckedi: "images/deneme.png"
imgSrcköpek: "images/deneme.png"
imgSrctavşan: "images/deneme.png"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.