Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to use an AngularJS $scope as an HTML attribute rather than viewable text.

main.js

var myApp = angular.module('myApp');

myApp.controller("buttonCtrl", ['$scope', function($scope){

$scope.johnny = [
    {quote: 'Anything for My Princess', controller: 'Princess'}
];

}]);

page1.html

<button ng-repeat="button in johnny" 
    ng-class="dynamic" 
    class="topcoat-button"
    ng-controller="{{button.controller}}"   <---- this is what does not work 
    ng-click="play()">
    {{button.quote}}
</button>

How can I fix this so I can add these variables as an attribute value.

Thanks

share|improve this question
    
possible duplicate of AngularJS: dynamically assign controller from ng-repeat –  karaxuna Mar 23 at 8:08
add comment

1 Answer 1

up vote 1 down vote accepted

Angular.js is a bit weird when doing this, but this should work. Also you're using ng-repeat wrong, but it's fixed below.

<button ng-repeat="johnny in buttons" 
    ng-class="dynamic" 
    class="topcoat-button"
    ng-controller="this.johnny.controller"
    ng-click="play()">
    {{johnny.quote}}
</button>
share|improve this answer
    
Hi, Thank you for responding. when I put in this.johnny.controller the button I see {{johnny.quote}} instead of the value. Also, I can only see the buttons if johnny is after "in". –  breadedturtle Mar 23 at 8:41
    
Ah yes, it is buttons in johnny, but I need to use {{button.quote}}; however, ng-controller is not working. I have updated my question so it is correct. –  breadedturtle Mar 23 at 8:49
    
Ohh, I took out the quotes on Princess and that solved it. Thank you very much for leading me to the answer! –  breadedturtle Mar 23 at 8:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.