如何在Angularjs控制器中设置动态变量名称

时间:2014-07-25 14:20:30

标签: angularjs

这是我的控制器代码:

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

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

});

目标是能够在视图中访问{{customerName}}和{{customerAddress}}。 任何人都可以告诉我这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:24)

改为使用$scope[field] = value;

使用$scope.field,您正在创建名为field的新属性。

答案 1 :(得分:0)

   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"