ให้ความสำคัญกับอินพุต angularjs ไม่ทำงาน

ฉันกำลังพยายามสร้างคำสั่งโฟกัสเมื่อฉันเปิดหน้าเว็บที่อยู่ภายในแบบฟอร์มในข้อความอินพุตแรกที่ยื่น นี่คือคำสั่ง

app.directive("getFocus", function() {
    return {
        restrict: 'AC',
        link: function(scope, element){
            console.log("focus!");
            element.focus();
        }
    }
});

และในอินพุต html ของฉัน

<input id="ss" data-ng-model="item.value" type="text" data-ng-class="{'get-focus': $first}">
<input id="tt" type="text" data-ng-model="item.value" data-ng-class="{'get-focus': $first}">

ฉันใช้ ng-class เพราะเป็นรูปแบบไดนามิกที่สร้างการวนรอบ json และฉันไม่รู้ว่าอินพุตใดถูกสร้างขึ้นเป็นอันดับแรก ดูเหมือนว่าคำสั่งจะไม่ทำงาน Infact, console.log ไม่ปรากฏในคอนโซล.. มีอะไรผิดปกติหรือเปล่า?


person Atlas91    schedule 12.03.2015    source แหล่งที่มา
comment
คำสั่งคลาสจะไม่ทำงานกับ ng-class สามารถใช้ได้เฉพาะกับแอตทริบิวต์คลาส html เท่านั้น   -  person Vinay K    schedule 12.03.2015
comment
แม้ว่าฉันจะใช้ข้อ จำกัด : 'AC'?   -  person Atlas91    schedule 12.03.2015
comment
ใช่. ในกรณีข้างต้น getFocus คำสั่งจะถูกเพิ่มตามเงื่อนไข เราจำเป็นต้องคอมไพล์โหนดอีกครั้งหลังจากเพิ่มคลาสเพื่อให้มันทำงานได้   -  person Vinay K    schedule 12.03.2015


คำตอบ (1)


ฉันมีปัญหาเช่นนั้นและฉันก็จบลงด้วยคำสั่งนี้

angular.module('myApp').directive('autofocusIf', function($timeout) {
return {
  restrict: 'A',
  scope: {
    autofocusIf: '&'
  },
  link: function($scope, $element) {
    $scope.$watch('autofocusIf', function(shouldFocus) {
      if (shouldFocus()) {
        $timeout(function() {
          $element[0].focus();
        });
      } else {
        $timeout(function() {
          $element[0].blur();
        });
      }
    });
  }
};
})

แล้วใน html

<div ng-repeat="repeat in repeats">
  <input type="text" autofocus-if="$first" ng-model="myModel" />
</div>
person maurycy    schedule 12.03.2015
comment
คุณป้อนข้อมูลของคุณไว้ใน ng-repeat คุณสามารถสร้างเสียงระเบิดได้ไหม? - person maurycy; 12.03.2015
comment
โอเค ฉันจะไป.. จะมีปัญหาไหมถ้าฉันใช้ uikit? - person Atlas91; 12.03.2015
comment
ฉันไม่คิดอย่างนั้น แม้ว่าฉันจะได้สร้างเครื่องมือระเบิดที่ใช้งานได้สำหรับคุณแล้ว plnkr.co/edit /ohJ62hl0rtNb1NNus9F6?p=ดูตัวอย่าง - person maurycy; 12.03.2015
comment
ฉันคิดว่าฉันพบปัญหา..หรืออาจจะไม่..คุณได้ตั้งค่า ng-model เป็นอินพุตแล้ว.. ในกรณีของฉัน ฉันมี ng-model อีกอันหนึ่ง.. ดูการแก้ไขของฉัน - person Atlas91; 12.03.2015
comment
เป็นดังนี้: plnkr.co/edit/c0fyixdoAOmnYVilnFrO?p=preview และที่นี่ ไม่ทำงาน - person Atlas91; 12.03.2015
comment
นักวางระเบิดนั้นใช้งานได้สำหรับฉัน ฉันไม่คิดว่า ng-model จะเป็นปัญหาที่นี่ - person maurycy; 12.03.2015
comment
ก็..มันแปลกๆนะ :( - person Atlas91; 12.03.2015