ng-click () dan olingan HTML elementlarining sinflarini qanday o'zgartirish mumkin

HTML elementim bor:


va men nazoratchi:

$scope.run() = function($event) {
   var el = $event.currentTarget;
}

endi qanday qilib el elementida ba'zi sinflarni qo'shishni xohlasam bo'ladi, lekin el.addClass ("class-name") ishlamaydi

Faqat javascriptni bu erda jQuery emas, balki yordamida ham foydalanishim mumkin

0
el.classList.add ("className") . Bundan tashqari ng-class dan foydalanishingiz mumkin va siz bosish orqali yangilanadigan bayroqni ko'rishingiz mumkin
qo'shib qo'ydi muallif Rajesh, manba

7 javoblar

ng-class="expression"

U sinfni qo'shish uchun tekshiruvchi o'rniga ng-class dan foydalanishni tavsiya qiladi.

lekin agar xohlasangiz, element.classList.add ("className") dan foydalanishingiz mumkin

2
qo'shib qo'ydi

classList API yordamida elementlarni sinflarga o'zgartirganda, classList nom maydonidan foydalaning. ya'ni,

el.classList.add('class-name');

Siz hali ham jQuery-da o'ylayotganga o'xshaysiz.

1
qo'shib qo'ydi

You can use ng-class here is the demo Jsfiddle demo

Js kodi

  var app = angular.module('app', []);
  app.controller('ctrl', function($scope) {
    $scope.myClass = false;
    $scope.addClass = function() {
      $scope.myClass = true;
    }
  });

HTML

  <div ng-app="app">
    <div ng-controller='ctrl'>
      This my content

      <button ng-click='addClass()'>
        Add class
      </button>
    </div>
  </div>
1
qo'shib qo'ydi

html:


JS:

$scope.run() = function(event) {
   var element = angular.element(event.currentTarget);
   element.addClass('new');
 }
1
qo'shib qo'ydi

Buni amalga oshirish uchun ko'proq mos usul deb o'ylayman, siz ng-class kodini i elementingiz uchun ishlatishingiz va o'zingizning ba'zi o'zgaruvchingizga bog'lashingiz kerak.

Misol:

Sizning HTML:


Burchak tekshirgichingiz:

function Controller($scope){

 $scope.myClass = "";
 $scope.changeClass = function() {
     $scope.myClass = //Put here your CSS class
}

}

1
qo'shib qo'ydi

Ng-classni angular JS dan foydalanishingiz mumkin


tekshirgichda

$scope.run() = function(event) {
  $scope.myclass="classname";
 }
1
qo'shib qo'ydi

Bunga oddiygina HTML qo'shing

i

Ko'zda tutilgan

 $scope.run = function($event) {
            $event.target.classList.add("className")
        }
1
qo'shib qo'ydi
Angular Uzbekistan
Angular Uzbekistan
107 ishtirokchilar

Guruh Angular bo'yicha muloqot qilish uchun ochilgan Sizni qiziqtirsa: @nodejs_uz @react_uz @angular_uz @yiiframework_uz @js_uzb @typescript_uzb @vuejs_uz @ngTashkent @laravel_uz @uzdevgroup Ish o'rinlari @UzDev_jobs

Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb

ngTashkent
ngTashkent
77 ishtirokchilar

Правила просты: 1. Не хамить 2. Не хейтить 3. Реакт не нужен Ссылки: https://t.me/angular_ru - старший брат https://t.me/angular_uz - ангулар на узбекском @js_uzb @vuejs_uz @react_uz @nodejs_uz @yiiframework_uz @uzdevgroup @UzDev_Jobs @tasdev_talks