简体   繁体   English

使用输入更新ng模型数据

[英]Update ng-model data with inputs

I'm new in angular and I have a problem. 我是新手,遇到问题。

I want get promise data from DB and show it by input. 我想从数据库中获取承诺数据并通过输入显示它。

This is my input: 这是我的输入:

    // not editable
    <input type="text" ng-model="currentMedCenter.name"/>

This is my controller with 'currentMedCenter' model: 这是我的带有“ currentMedCenter”模型的控制器:

    function AccountProfileController($scope, medicalCenterService, doctorService) {
        //get information from singleton service
        $scope.currentMedCenter = medicalCenterService.currentMedCenter;
    }

I get data from DB by $http.get, which wrapped in singleton service. 我通过$ http.get从数据库获取数据,该数据包装在单例服务中。

This is singleton medicalCenterService: 这是单例医疗中心服务:

.factory('medicalCenterService', function (MedicalCenterResource) {

    var medicalCenterService = {};

    medicalCenterService.currentMedCenter = MedicalCenterResource.getCurrentMedicalCenter();

    return medicalCenterService;
})

This is query to server: function 'getCurrentMedicalCenter()': 这是对服务器的查询:函数'getCurrentMedicalCenter()':

getCurrentMedicalCenter: function () {
      var deferred = $q.defer();

            $http.get('Api/MedicalCenter/GetCurrentMedicalCenter').success(function(data) {
                deferred.resolve(data);
            });

            return deferred.promise;
}

After query on server, I get JSON object as follow: 在服务器上查询后,我得到JSON对象,如下所示:

  {
    "id": 1,
    "name": "Medical center #1"
  }

This object successfully stored in singleton service medicalCenterService.currentMedCenter. 该对象已成功存储在单例服务medicalCenterService.currentMedCenter中。 And medical center name show in input on UI-side. UI侧的输入中将显示医疗中心名称。

I want update my 'currentMedcenter' model, but when I try to edit value in input, I can not change anything in it. 我想更新我的“ currentMedcenter”模型,但是当我尝试编辑输入中的值时,我无法对其进行任何更改。 I can not add new symbol or delete previous. 我无法添加新符号或删除先前的符号。

I'm using 1.0.8 angular version. 我正在使用1.0.8角版本。

First, rewrite currentMedCenter to be : 首先,将currentMedCenter重写为:

getCurrentMedicalCenter: function () {
   return $http.get('Api/MedicalCenter/GetCurrentMedicalCenter');
}

No need to create a new promise here. 无需在这里创建新的承诺。

Then in your factory change : 然后在您的工厂进行更改:

medicalCenterService.currentMedCenter = MedicalCenterResource.getCurrentMedicalCenter();

to be : 成为 :

MedicalCenterResource.getCurrentMedicalCenter().success(function(data) {
   medicalCenterService.currentMedCenter = data;
});

So you set currentMedCenter to be the value of the promise rather than the promise itself. 因此,您将currentMedCenter设置为承诺的值,而不是承诺本身。

EDIT : 编辑:

The next problem is that you are binding the input to currentMedCenter. 下一个问题是您将输入绑定到currentMedCenter。 At first this is just an empty object. 首先,这只是一个空对象。 Then when the http service returns the variable currentMedCenter is then being changed to point to the new data, meanwhile the input still has the value of the pointer to the old data and so doesn't get the update. 然后,当http服务返回变量currentMedCenter时,将其更改为指向新数据,同时输入仍然具有指向旧数据的指针的值,因此不会获取更新。

To fix this you need to pass an object to the controller that isn't going to change. 要解决此问题,您需要将一个不会更改的对象传递给控制器​​。 The object should contain a property that points to currentMedCenter. 该对象应包含一个指向currentMedCenter的属性。 As this object doesn't change the controller will pick up the fact that the objects property does change. 由于此对象未更改,因此控制器将获得对象属性确实发生更改的事实。

Pointers can catch you out, even in Javascript! 指针甚至可以用Java捕获您!

Change the factory to be : 将工厂更改为:

.factory('medicalCenterService', function (MedicalCenterResource) {

    var medicalCenterService = {currentMedCenter: {data: {}}};
    MedicalCenterResource.getCurrentMedicalCenter().then(function(d) {
        medicalCenterService.currentMedCenter.data = d;    
    });
    return medicalCenterService;
})

Note how the returned object now has a property data and it is this property that gets set when the promise resolves. 请注意,现在返回的对象是如何具有属性数据的,并且在promise解析时将设置此属性。

Then when binding the input : 然后在绑定输入时:

<input type="text" ng-model="currentMedCenter.data.name"/>

See this jsFiddle 看到这个jsFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM