AngularJS ng-change无法正常工作

时间:2016-05-05 04:08:42

标签: javascript angularjs angularjs-ng-change

我在$dialogScope.items中有这个下拉列表但我尝试调用函数selectChange但它似乎没有工作。请帮我注意一下这个错误

这是来自html部分

<div class="form-group has-feedback">
 <label class="control-label">Type</label><br/>
  <select class="form-control" ng-model="selectedItem" ng-change="selectChange()" ng-options="item as item.name for item in items">
   <option value=""> Select Type</option>
  </select>
</div>

已更新

 $scope.addStep = function (patchID) {
    $dialog.open({
        showClose: false,
        closeByEscape: true,
        template: 'views/app-edit/app-edit-patch-step-add.html',
        controller: ['$scope', function ($dialogScope) {
            $dialogScope.items = [{
                name:"Download APK",
                value:"0",
            },{
                name:"Backup",
                value:"1"
            },{
                name:"Restore",
                value:"2",
            },{
                name:"Download OBB",
                value:"4",
            },{
                name: "Download OBB By GPU",
                options : ["Adreno","Mali","Tegra","PowerVR","Other"]
            },{
                name: "Download APK By GPU",
                options : ["Adreno","Mali","Tegra","PowerVR","Other"]
            },{
                name: "Download CACHE",
                value:"7",
            },{
                name: "Download CACHE By GPU",
                value:"8",
            },{
                name: "Download CACHE & Unzip After Install",
                value:"9",
            },{
                name: "Download CACHE By GPU & Unzip After Install",
                value:"10",
            },
            ];
            $dialogScope.hideMe = function(hideElements){
                if($dialogScope.selectedItem){
                    return (hideElements.indexOf($dialogScope.selectedItem.name) != -1)?false:true;
                }
                else{
                    return true;
                }
            }
            $dialogScope.selectChange = function(selectedItem){
                if (selectedItem.value) {
                    $dialogScope.type = selectedItem.value;
                    $dialogScope.labelA = 'dferfre';
                    $dialogScope.labelB = '';
                    $dialogScope.labelC = 'MD5';
                    $dialogScope.stepA = '';
                    $dialogScope.stepB = '';
                    $dialogScope.stepC = '';
                    if (value == 0) {
                         $dialogScope.labelA = "APK URL";
                     } else if (value == 4) {
                         $dialogScope.labelA = "OBB URL";
                     } else if (value == 5) {
                         $dialogScope.labelB = "OBB URL";
                     } else if (value = 6) {
                         $dialogScope.labelB = "APK URL";
                     }


                    $dialogScope.$apply();
                }
            };

2 个答案:

答案 0 :(得分:1)

您应该将$ dialogScope.selectChange更改为$ scope.selectChange,并且您也忘记将该变量传递给ur html中的函数。我个人更喜欢使用controllerAs语法。

而不是具有内联控制器功能。创建单独的控制器“MyDialogController”文件,然后使用以下html替换ur html。

<div ng-controller="MyDialogController" class="form-group has-feedback">
 <label class="control-label">Type</label><br/>
  <select class="form-control" ng-model="selectedItem" ng-change="selectChange()" ng-options="item as item.name for item in items">
   <option value=""> Select Type</option>
  </select>
</div>

答案 1 :(得分:1)

将模型传递给您的函数。试试这个。

<select class="form-control" ng-model="selectedItem" ng-change="selectChange(selectedItem)" ng-options="item as item.name for item in items">