AngularJS使用ng-click来改变$ scope" self"值

时间:2015-08-17 22:11:08

标签: javascript angularjs

我正在尝试使用ng-click来更改单击的元素本身,并使用新值重新呈现自己。

控制器:

$scope.Keyboard = {
    click : function(self){
        self.active = false;
    }
};

$scope.keyboards = [
    {'id':1,'chars':[{'char':'a','active':true},{'char':'b','active':true},{'char':'c','active':true}]},
    {'id':2,'chars':[{'char':'d','active':true},{'char':'e','active':true},{'char':'f','active':true}]}
];

HTML:

<ul ng-repeat="keyboard in keyboards" id="{{keyboard.id}}" class="keyboard">
     <li ng-repeat="char in keyboard.chars" ng-click="Keyboard.click(this)" class="char {{char.active ? '' : 'deactivated'}}">{{char.char}}</li>
</ul>

循环和初始渲染是可以的。但是&#34; self.active = false&#34;并没有强制重新渲染。

我也试过$ apply但没有成功

1 个答案:

答案 0 :(得分:0)

如果您正在做的事情,那么就这样做会更简单:

Transclude In AngularJS

<ul ng-repeat="keyboard in keyboards" id="{{keyboard.id}}" class="keyboard">
  <li ng-repeat="char in keyboard.chars"
    ng-click="char.active = false"
    class="char {{char.active ? '' : 'deactivated'}}">
    {{char.char}}
    {{char.active}}
  </li>
</ul>

CTRL:

$scope.keyboards = [
    {'id':1,'chars':[{'char':'a','active':true},{'char':'b','active':true},{'char':'c','active':true}]},
    {'id':2,'chars':[{'char':'d','active':true},{'char':'e','active':true},{'char':'f','active':true}]}
];