Angular 1.x通过特定ID启用ng-style到元素

时间:2018-01-23 23:02:36

标签: javascript angularjs

鉴于具有相同ng-style属性的3个元素,有没有办法控制ng-style实际影响哪个元素?场景如下:用户可以与屏幕上的元素进行交互。在选择元素(通过单击它)后,用户可以使用input连接到ng-style ng-model来控制其宽度x,y位置。每个元素都有一个不同的ID,传递给ngclick函数(1,2或3)

  $scope.selectClickedElm = function(elm){
        console.log(elm);
        clickedElement = elm;
      //enable ng-style for element with the selected ID (1\2\3)
  }

function createAndAppend(element){
    var element;
   if(creationObject[element]==true){
   }else{
    if(element =="input"){
        element = angular.element("<"+element+" id='inputTag' ng-click='selectClickedElm(1)' ng-style='{width: w + \"px\",top: t + \"px\",left: l + \"px\"}' placeholder='input'>");
        angular.element(document.getElementById('board')).append($compile(element)($scope));
    }else if(element =="label"){
        element= angular.element("<"+element+" id='labelTag' ng-click='selectClickedElm(2)' ng-style='{width: w + \"px\",top: t + \"px\",left: l + \"px\"}'>{{txt || 'Label'}}</"+element+">");
        angular.element(document.getElementById('board')).append($compile(element)($scope));
    }else{
        element= angular.element("<"+element+" id='buttonTag' ng-click='selectClickedElm(3)' ng-style='{width: w + \"px\",top: t + \"px\",left: l + \"px\"}'>{{txt || 'Button'}}</"+element+">");
        angular.element(document.getElementById('board')).append($compile(element)($scope));
    }
   }
}

0 个答案:

没有答案