简体   繁体   中英

How can I make a button title equal to a value on my scope?

I have a button inside of an ng-repeat div on my template, and I would like to set it's title property to be equal to a value from my scope, but it is showing up as the string, not the value.

template:

<div ng-repeat="step in steps">
  <button title="step.description">Text</button>
</div>

On hover, the above displays the literal string "step.description" instead of the value from my scope. Currently my controller has an array on it called steps, and each of the step objects in that array has a key named "description" set to a different string.

I think this link can help you. How do you get AngularJS to bind to the title attribute of an A tag?

<body ng-app="myApp">

    <div ng-controller="myCtrl">
          <div ng-repeat="step in steps">
          <button ng-attr-title="{{step.description}}">Text</button>
      </div>
    </div>  
  </body>

You can use ng-attr-title to bind value on hover. ng-attr is a new directive in AngularJS 1.1.4

 app.controller('myCtrl', ['$scope', function($scope){
        $scope.steps = [
          {
          description: 'Test1'
        },
         {
          description: 'Test2'
        },
         {
          description : 'Test3'
        }

        ];

Here is the link: https://plnkr.co/edit/Ql4GHSkcuD1r9SywL2mi?p=preview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM