2016-09-20 77 views
0

指令AngularJS - 在指令動態開關罩

myApp.directive("progressingIndex", function ($compile, $rootScope, constant) { 


    var linker = function (scope, element, attrs) { 
    var Id = $rootScope.Obj.pictureId; 

switch (Id) { 
     case constant.picturebox00: $rootScope.Obj.progressing00 = true; break; 
     case constant.picturebox01: $rootScope.Obj.progressing01 = true; break; 
     case constant.picturebox02: $rootScope.Obj.progressing02 = true; break; 
     case constant.picturebox03: $rootScope.Obj.progressing03 = true; break; 
     case constant.picturebox10: $rootScope.Obj.progressing10 = true; break; 
     case constant.picturebox11: $rootScope.Obj.progressing11 = true; break; 
     case constant.picturebox12: $rootScope.Obj.progressing12 = true; break; 
     case constant.picturebox13: $rootScope.Obj.progressing13 = true; break; 
     case constant.picturebox20: $rootScope.Obj.progressing20 = true; break; 
     case constant.picturebox21: $rootScope.Obj.progressing21 = true; break; 
     case constant.picturebox22: $rootScope.Obj.progressing22 = true; break; 
     case constant.picturebox23: $rootScope.Obj.progressing23 = true; break; 
     case constant.picturebox30: $rootScope.Obj.progressing30 = true; break; 
     case constant.picturebox31: $rootScope.Obj.progressing31 = true; break; 
     case constant.picturebox32: $rootScope.Obj.progressing32 = true; break; 
     case constant.picturebox33: $rootScope.Obj.progressing33 = true; break; 
} 

    $compile(element.contents())(scope); 
    }; 
    return { 
     restrict: "E", 
     link: linker 
    }; 
}); 

這裏var Id讓我對0001值等如何動態地通過附加Id生成switch語句的情況?

回答

1
switch(constant['picturebox'+Id]) { 
    case constant['picturebox'+Id]: $rootScope.Obj['progressing'+Id] = true; break; 
} 

這足以測試所有情況。