选择NG选项更改时获得价值 [英] Get value when selected ng-option changes

查看:125
本文介绍了选择NG选项更改时获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的.html页面下拉列表,

I have in my .html page a dropdown list,

下拉:

 <select  ng-model="blisterPackTemplateSelected" data-ng-options="blisterPackTemplate as blisterPackTemplate.name for blisterPackTemplate in blisterPackTemplates">
     <option value="">Select Account</option>
 </select>

我要执行,当用户选择一个值的动作。所以,在我的控制器我所做的:

I want to execute an action when the user select a value. So In my controller I did:

控制器:

 $scope.$watch('blisterPackTemplateSelected', function() {
     alert('changed');
     console.log($scope.blisterPackTemplateSelected);
 });

但改变在DropDownList值不会触发code: $ $范围手表('blisterPackTemplateSelected',函数()

因此​​,我试着用另一种方法: ng_change ='changedValue()在选择标签

As a result I tried another method with a : ng_change='changedValue()' on the select tag

功能:

 $scope.changedValue= function() {
 console.log($scope.blisterPackTemplateSelected);
 }

但是blisterPackTemplateSelected存储到一个子范围。我读的父母无法获得对该子女的探视范围。

But the blisterPackTemplateSelected is stored into a child scope. I read that the parent can't get access to the child scope.

什么是执行一些正确的/最好的方式,当在下拉列表改变选择的价值呢?如果它的方法1,我在我的code做错了吗?

What is the correct/best way to execute something when a selected value in a dropdown list changes? If it's method 1, what am I doing wrong with my code?

推荐答案

作为阿尔乔姆说你需要使用 ngChange ,并通过ngModel对象作为参数传递给你的ngChange功能

as Artyom said you need to use ngChange and pass ngModel object as argument to your ngChange function

示例

<div ng-app="App" >
 <div ng-controller="ctrl">
<select  ng-model="blisterPackTemplateSelected" ng-change="changedValue(blisterPackTemplateSelected)" 
     data-ng-options="blisterPackTemplate as blisterPackTemplate.name for blisterPackTemplate in blisterPackTemplates">
    <option value="">Select Account</option>
</select>
{{itemList}}     
  </div>       
</div>

JS:

function ctrl($scope){
    $scope.itemList=[];
    $scope.blisterPackTemplates=[{id:1,name:"a"},{id:2,name:"b"},{id:3,name:"c"}]

    $scope.changedValue=function(item){
    $scope.itemList.push(item.name);
    }       
}

活生生的例子: http://jsfiddle.net/choroshin/9w5XT/4/

这篇关于选择NG选项更改时获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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