AngularJS 使用$scope.(参数)

AngularJS Use $scope.(parameter)

本文关键字:参数 scope 使用 AngularJS      更新时间:2023-09-26

我有 2 个buttons和 2 个contenteditable div。当我单击一个button时,我传递一个参数。现在在控制器中,我有一个 $rootScope.$on() 函数,它附加到contenteditable div中,所以我想在 $rootScope.$on() 方法中编写$scope.paramter,而不是参数,我想把我传递的值作为参数。

例:

<button type="button" ng-click="open('textModel')">Emojis</button>
<div contenteditable="true" ng-model="textModel"></div>
<button type="button" class="btn btn-default" ng-click="open('textModel2')">Emojis</button>
<div contenteditable="true" ng-model="textModel2"></div>

控制器内部

$rootScope.$on('selectedCode', function(event, args){
   console.log(btnid);  //btnid has the value textModel or textModel2 depending on the button clicked
    $scope.btnid += 'Hello';  //I want that instead of btnid, textModel gets appended
});

现在在控制器中我想把它写成$scope.textModel += 'Hello';而不是$scope.parameter,所以当我按下第一个按钮时,ng-model 作为 textModel 的contenteditable div被附加,当我按下第二个按钮时,ng-model 作为 textModel2 的contenteditable div被附加

请帮忙

使用括号表示法按变量名称访问属性:

$rootScope.$on('selectedCode', function(event, args) {
    $scope[btnid] += 'Hello';
});