1

I have 2 buttons and 2 contenteditable div. When I click a button, I pass a parameter. Now in the controller, I have a $rootScope.$on() function which appends in the contenteditable div so I want to write $scope.paramter in the $rootScope.$on() method, and in place of parameter I want to put the value I passed as the parameter.

Example:

<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>

//Inside controller

$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
});

Now in the controller I want to write it as $scope.textModel += 'Hello'; instead of $scope.parameter, So tht when I press first button, the contenteditable div with ng-model as textModel gets appended and when I press second button, the contenteditable div with ng-model as textModel2 gets appended

Please Help

1 Answer 1

1

Use bracket notation to access property by variable name:

$rootScope.$on('selectedCode', function(event, args) {
    $scope[btnid] += 'Hello';
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.