angularjs - 通过对NG-改变所选对象的其他属性 [英] angularjs - pass selected object's other properties on ng-change

查看:269
本文介绍了angularjs - 通过对NG-改变所选对象的其他属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能从加载JSON在下拉/ NG选项其他属性。

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

在NG-变化我也喜欢通过所选对象的campaignType。

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

我将如何能做到这一点?

How would I able to do that?

我的看法是这样看

  <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/

推荐答案

检查了这一点:的http://的jsfiddle .NET / Qgmz7 / 9 /

您是不是在ngRepeat背景。 ngOptions的作品完全不同。

You are not in an ngRepeat context. ngOptions works totally different.

ngModel在&LT;选择&GT; 将获得&LT值;选项&GT; ,而不是整个目的。同样,你不是一个ngRepeat里面,你不必你的对象的访问。

ngModel on a <select> will get the value of the <option>, not the whole object. Again, you are not inside an ngRepeat, you don't have access to your objects.

没有必要通过值作为参数,你可以从 $范围得到它。作为事实上你不需要ngChange或者,你可以直接 $范围。$腕表('运动',...)

No need to pass the value as parameter, you can get it from $scope. As a matter of fact you don't need ngChange either, you can just $scope.$watch('campaign', ...)

所以

$scope.search2 = function () {
    console.log($scope.campaign);
}

这篇关于angularjs - 通过对NG-改变所选对象的其他属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆