How to overwrite existing scopes using angular select

时间:2017-06-14 10:29:28

标签: javascript angularjs

I'm making a system that allows for drafts in my ionic and angular app.

Some values are automatically set when opening the page, now i try to overwrite them using the draft data like this.

<select name="" ng-model="draft" ng-change="loadDraft(draft);">
   <option ng-repeat="draft in drafts" value="{{draft.info}}">{{draft.info.Ordernumber}}</option>
</select>

And my controller code:

  $scope.loadDraft = function(draft) {
        if(draft){
          var parsed = JSON.parse(draft);
          vm.infoFactory.ArrayInfo = parsed;
          $scope.info = parsed;
        }else {
          console.log("oops");
        }
  }

Problem is that it only loads the "new" values and does not overwrite things like the order number that are automatically set on page load.

How do i force all values to update to the loaded array information.

--- edit --- I just found out that when setting the initial scope by hand it does overwrite the value just fine. Problem is probably in the way i set the scopes on page load

this does overwrite fine.

vm.infoFactory.ArrayInfo.Ordernumber = "2017-33";

when adding this piece of code it doesnt

if(vm.infoFactory.ArrayInfo.OrdernumberSet == true){
        console.log("all set");
    }
    else{
        console.log("New Date");
        var date = new Date();
        vm.infoFactory.ArrayInfo.currentYear = date.getFullYear();
        var Ordervalue = firebase.database().ref("/savednumbers/" + vm.infoFactory.ArrayInfo.currentYear);
        $scope.$watch("info", function(newValue, oldValue) {
            Ordervalue.on("value", function(snapshot) {
              var data = snapshot.val();
              vm.infoFactory.ArrayInfo.Ordervalue = data.last_number;
              vm.infoFactory.ArrayInfo.Ordervalue++;
              vm.infoFactory.ArrayInfo.Ordernumber = vm.infoFactory.ArrayInfo.currentYear + "-" + vm.infoFactory.ArrayInfo.Ordervalue;
            });
        });   
        vm.infoFactory.ArrayInfo.OrdernumberSet = true;
    }              

probably should open a new question for this?

--- edit --- The value is okay on "draft" but after my JSON.parse it takes the new value

1 个答案:

答案 0 :(得分:1)

如果要访问ng-change函数中的整个对象,则将整个对象分配给值

  <option ng-repeat="draft in drafts" value="{{draft}}">{{draft.info.Ordernumber}}</option>