最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS: Passing object property name to directive - Stack Overflow

programmeradmin2浏览0评论

I'm writing an angular.js directive that is a reusable input ponent for an array of objects.

Since it is impossible to use primitive values in ng-repeat (see: What is the angularjs way to databind many inputs?), I have to pass an array of objects to the ponent:

In the controller I initialize:

$scope.theSimpsons = [{ value: 'Bart' }, { value: 'Lisa' }];

And then use it in the HTML file:

<div ng-app="App">
    <div ng-controller="AppCtrl">
        <multi-input items="theSimpsons" />
    </div>
</div>

The directive itself is implemented like this:

directive('multiInput', function () {
return {
    restrict: 'E',
    scope: {
        items: '=items'
    },
    template:
        '<div>' +
        '<div ng-repeat="item in items">' +
        '<input type="text" ng-model="item.value">' +
        '<a ng-click="items.splice($index, 1)">remove</a>' +
        '</div>' +
        '<a ng-click="items.push({value:\'\'})">add</a>' +
        '</div>'
};
});

This is all working good.

My question: what if the objects don't have a value?

This directive codes the name of the property (value) hard. But what, if I want to have an array like this: [{ name: 'Bart' }, { name: 'Lisa' }].

Is it possible to pass the name of the object, e.g. like

<multi-input items="names" property="name"></multi-input>

and use it somehow in the directive to access the name property?

Here is the JSFiddle / I have created to show this directive.

I'm writing an angular.js directive that is a reusable input ponent for an array of objects.

Since it is impossible to use primitive values in ng-repeat (see: What is the angularjs way to databind many inputs?), I have to pass an array of objects to the ponent:

In the controller I initialize:

$scope.theSimpsons = [{ value: 'Bart' }, { value: 'Lisa' }];

And then use it in the HTML file:

<div ng-app="App">
    <div ng-controller="AppCtrl">
        <multi-input items="theSimpsons" />
    </div>
</div>

The directive itself is implemented like this:

directive('multiInput', function () {
return {
    restrict: 'E',
    scope: {
        items: '=items'
    },
    template:
        '<div>' +
        '<div ng-repeat="item in items">' +
        '<input type="text" ng-model="item.value">' +
        '<a ng-click="items.splice($index, 1)">remove</a>' +
        '</div>' +
        '<a ng-click="items.push({value:\'\'})">add</a>' +
        '</div>'
};
});

This is all working good.

My question: what if the objects don't have a value?

This directive codes the name of the property (value) hard. But what, if I want to have an array like this: [{ name: 'Bart' }, { name: 'Lisa' }].

Is it possible to pass the name of the object, e.g. like

<multi-input items="names" property="name"></multi-input>

and use it somehow in the directive to access the name property?

Here is the JSFiddle http://jsfiddle/napU6/5/ I have created to show this directive.

Share Improve this question edited May 23, 2017 at 11:45 CommunityBot 11 silver badge asked May 1, 2013 at 14:39 Moritz PetersenMoritz Petersen 13.1k3 gold badges41 silver badges46 bronze badges 1
  • Your question helped me in my problem $scope.theSimpsons = [{ value: 'Bart' }, { value: 'Lisa' }]; kuddos – Usman Iqbal Commented Dec 28, 2016 at 10:48
Add a ment  | 

1 Answer 1

Reset to default 4

Use another attribute to pass the name of the property

<multi-input items="myItems" name="value"></multi-input>

Directive

app.directive('multiInput', function(){
  return {
    restrict: 'E',
    replace: true,
    scope: {
      items:'=',
      name: '@'
    },
    template:'<div>'
      + '<div ng-repeat="item in items">'
      + '<input type="text" ng-model="item[name]">'
      + '<a ng-click="items.splice($index, 1)">remove</a>'
      + '</div>'
      + '<a ng-click="items.push({})">add</a>'
      + '</div>'
  }
});

Demo: Plunker

发布评论

评论列表(0)

  1. 暂无评论