2017-07-17 63 views
0

我尝试了所有我发现的方式,但它不工作,我只是简单地尝试以角度js方式获取其更改事件的下拉选定选项所以它会被传递给ajax并从数据库中获取数据。我的项目中有angular.min.js 1.4.8版本的cdn。无法获取角度控制器上的下拉列表的选定值在其更改事件

我复制/粘贴我的代码到plunker那里工作正常,但在浏览器中运行我的项目时,它不工作。下面是我的JS和HTML代码片段。

JS代码

$scope.inputs = [{ 
    id : 1, 
    name : 'option1', 
    value : 'option1', 
    inputType : 'tag', 
    valueOperator : "option1Operator", 
    operatorType : 'operatorType1' 
    }, { 
    id : 2, 
    name : 'option2', 
    value : 'option2', 
    inputType : 'text', 
    valueOperator : "option2Operator", 
    operatorType : 'operatorType1' 
    }]; 
$scope.inputSelectedChange = function(){ 
     $scope.$watch('inputSelected',function(val){ 
       $http({ 
        method: 'GET', 
        url: '<<URL>>', 
        responseType: 'json', 
        params:{inputName: "prdSeqId"} 
       }).then(function successCallback(response) { 
        //something 
}, function errorCallback(response) { 
        // called asynchronously if an error occurs 
        // or server returns response with an error status. 
        }); 
}); 

HTML代码

<select ng-model="inputSelected" 
     ng-options="input as input.name for input in inputs" 
     ng-change="inputSelectedChange()" required> 
    <option value="">Select Input</option> 
</select> 
+0

获得所选项目的内部ng-change值不必使用$范围。$看你的NG-更改功能,因为ng-model值将在控制器中可用 – Vivz

+0

请阅读[在什么情况下可以添加“urgent”或其他类似的短语,以我的问题,以获得更快的答案?](/ meta.stackoverflow.com/q/326569) - 总结是,这不是一个理想的方式来解决志愿者,并可能适得其反。请不要将这添加到您的问题。 – halfer

+0

n-model值未定义,有或没有$ scope。$ watch,我试过了。谢谢 –

回答

0
//html 
<select ng-model="inputSelected" id="inputSelected" class="form-control positionTypes" ng-options="input as input.name for input in inputs" ng-change="inputSelectedChange(inputSelected)" required> 
    <option value="">Select Input</option> 
</select> 

//controller 
$scope.inputSelectedChange = function(value){ 
    console.log(value) 
}; 
+0

我传递了ng-change名称,而不是inputSelected,愚蠢的错误..谢谢你的解决方案.. :) –

0

尝试改变功能象下面这样: 并获得价值。

$scope.inputSelectedChange = function(){ 
    console.log($scope.inputSelected) 
    //do other stuff here 
} 
0

您可以从ng-model

var myApp = angular.module('myApp',[]); 
 

 
//myApp.directive('myDirective', function() {}); 
 
//myApp.factory('myService', function() {}); 
 

 
function MyCtrl($scope) { 
 
    $scope.inputs = [{ 
 
    id : 1, 
 
    name : 'option1', 
 
    value : 'option1', 
 
    inputType : 'tag', 
 
    valueOperator : "option1Operator", 
 
    operatorType : 'operatorType1' 
 
    }, { 
 
    id : 2, 
 
    name : 'option2', 
 
    value : 'option2', 
 
    inputType : 'text', 
 
    valueOperator : "option2Operator", 
 
    operatorType : 'operatorType1' 
 
    }]; 
 
$scope.inputSelectedChange = function(){ 
 
console.log($scope.inputSelected) 
 
} 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="MyCtrl"> 
 
<select ng-model="inputSelected" id="inputSelected" class="form-control positionTypes" ng-options="input as input.name for input in inputs" ng-change="inputSelectedChange()" required> 
 
    <option value="">Select Input</option> 
 
</select> 
 
</div>

相关问题