Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs - pass selected object's other properties on ng-change

Tags:

angularjs

How can I get other properties from loaded json in dropdown / ng-options

On ng-change I also like to pass selected object's campaignType.

How would I able to do that?

My View is looking like this

  <div ng-app>
    <div ng-controller="cCtrl">
        <select ng-model="campaign" ng-options="c.id as c.name for c in campaigns" ng-change="search2(c.campaignType)">
            <option value="">-- choose campaign --</option>
        </select>
    </div>
</div>

My Controller is looking like this

  function cCtrl($scope) {

    $scope.campaigns = [{
        "custID": 1,
        "custName": "aaa ",
        "msgID": 3,
        "msgName": "Email Test Msg",
        "id": 2,
        "name": "Email Test Campaign",
        "description": "Test Campaign",
        "campaignType": "Email",
        "created": "1374229715",
        "isActive": 1,
        "isDeleted": 0
    }];

 $scope.search2 = function (campaignType) {
        alert(campaignType); // not working
        alert($scope.campaign.campaignType); // not working
        //console.log($scope.campaign.campaignType);

    }

}

http://jsfiddle.net/webtheveloper/Qgmz7/8/

like image 799
Developer Avatar asked Sep 05 '13 15:09

Developer


1 Answers

Instead of passing in a property, you can pass the selected object into the function like this

<select ng-model="campaign" ng-options="c.name for c in campaigns" ng-change="search2(campaign)">

Working Demo

like image 101
zs2020 Avatar answered Oct 04 '22 16:10

zs2020