【问题标题】:AngularJS Scope as HTML attribute valueAngularJS 范围作为 HTML 属性值
【发布时间】:2014-04-30 13:37:24
【问题描述】:

我正在尝试将 AngularJS $scope 用作 HTML 属性而不是可视文本。

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>

如何解决这个问题,以便将这些变量添加为属性值。

谢谢

【问题讨论】:

标签: javascript html angularjs scope


【解决方案1】:

Angular.js 这样做时有点奇怪,但这应该可以工作。此外,您使用 ng-repeat 错误,但它已在下面修复。

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

【讨论】:

  • 您好,感谢您的回复。当我放入 this.johnny.controller 按钮时,我看到的是 {{johnny.quote}} 而不是值。另外,如果 johnny 在“in”之后,我只能看到按钮。
  • 啊,是的,它是 johnny 中的按钮,但我需要使用 {{button.quote}};但是,ng-controller 不起作用。我已经更新了我的问题,所以它是正确的。
  • 哦,我把Princess的名言去掉了,就解决了。非常感谢您引导我回答!
猜你喜欢
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 2013-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多