更改$ scope变量

时间:2015-08-19 08:47:56

标签: javascript angularjs

我有项目列表和模态窗口的表格,我可以更改药物属性。当属性改变时,药物必须从该列表中删除。但它没有删除。

模态窗口:

 $modal.open({
    templateUrl:'app/interactions/partials/modals/resolveDrug.html',
    controller: 'DrugsListController',
    scope: $scope
}).result.then(function(data){
    var index = _.findIndex($scope.drugs, {_id: data._id});
    $scope.drugs.splice(index,1);
}

我认为元素没有删除'因为当我打开模态窗口时我创建了我的范围副本然后使用副本..

在该表格上,我有刷新按钮,刷新所有列表。

 $scope.refresh= function() {
    $scope.drugs = UnresolvedDrugsService.query();
};

它也不起作用。我认为这是因为我也使用副本。 哦,我尝试发出一些事件

$modal.open({
    templateUrl:'app/interactions/partials/modals/resolveDrug.html',
    controller: 'DrugsListController',
    scope: $scope
}).result.then(function(data){
    var index = _.findIndex($scope.drugs, {_id: data.data._id});

    $rootScope.$emit('refreshDrug', index);

}

$rootScope.$on('refreshDrug', function(index){
    $scope.drugs = [];
    $scope.drugs.splice(index,1);
   // $scope.drugs= UnresolvedDrugsService.query();
});

它没有用。

你能帮助我并描述我做错了什么,谢谢!

UPD 模态窗口html

<form role="form" name="resolveDrugForm"  ng-submit="saveResolvedDrug(drug) && $close(drug)">
   ........{some code, input's and label}......
<input type="submit" class="btn btn-primary" value="{{'COMMON.SAVE' | translate}}"/>
<button type="button" class="btn btn-default" ng-click="$dismiss()" >{{'COMMON.CANCEL' | translate}}</button>

ng-repeat代码

<tr ng-repeat="drug in drugs" ng-click="resolveDrug($index)">
            <td>{{drug.productName || drug.description }}</td>
            <td>{{drug.aiccode  }}</td>
</tr>

和控制器的所有方法:

 $rootScope.$on('refreshDrug', function(index){
       // $scope.drugs = [];
        $scope.drugs.splice(index,1);
       // $scope.drugs= UnresolvedDrugsService.query();
    });


    $scope.drugs= UnresolvedDrugsService.query();

    $scope.refresh= function() {
        $scope.drugs= UnresolvedDrugsService.query();
    };
    $scope.spliceEl = function(data){
        var index = _.findIndex($scope.drugs, {_id: data._id});
        $scope.drugs.splice(index,1);
        return true;
    };
    $scope.saveResolvedDrug = function(drug){
        DrugsService.addResolvedDrug(drug).then(function(data){
            var index = _.findIndex($scope.drugs, {_id: data.data._id});
            if(data.data.ingredients && data.data.ingredients.length > 0){
                data.data.ingredients = JSON.parse(data.data.ingredients);
            }
            $scope.drugs.splice(index,1);
            return true;
        });
        return true;
    };

    $scope.resolveDrug=function(index){
        $scope.drug={};
        var drugs = $scope.drugs;
        $scope.drug=angular.copy($scope.drugs[index]);
        var scope=$scope;
        $modal.open({
            templateUrl:'app/interactions/partials/modals/resolveDrug.html',
            controller: 'DrugsListController',
            scope: $scope
        }).result.then(function(data){
                console.log($scope.drugs);
                var index = _.findIndex($scope.drugs, {_id: data._id});
                //$scope.drugs.splice(index,1);

                console.log($scope.drugs);
                $rootScope.$emit('refreshDrug', index);

            }, function(data){

            }).finally(function(data){
                $scope.refresh();
            });

    }

1 个答案:

答案 0 :(得分:0)

我不知道为什么它不能用于事件。但是如果saveDrug在模态结果但是sumbit形式 - 工作正常。 现在代码看起来像

   $scope.resolveDrug=function(index){
        $scope.drug={};
        var drugs = $scope.drugs;
        $scope.drug=angular.copy($scope.drugs[index]);
        var scope=$scope;
        $modal.open({
            templateUrl:'app/interactions/partials/modals/resolveDrug.html',
            controller: 'DrugsListController',
            scope: scope,
            resolve: {
                drug: function () {
                    return $scope.drug;
                }
            }
        }).result.then(function(data){
            }, function(data){

            }).finally(function(data){

            });

    }
   $scope.saveResolvedDrug = function(drug){
        DrugsService.addResolvedDrug(drug).then(function(data){
            var index = _.findIndex($scope.drugs, {_id: data.data._id});
            if(data.data.ingredients && data.data.ingredients.length > 0){
                data.data.ingredients = JSON.parse(data.data.ingredients);
            }
            $scope.drugs.splice(index,1);
            return true;
        });
        return true;
    };