简体   繁体   English

为什么此功能中的双向绑定不能按角度工作?

[英]Why is the two-way binding in this function not working in angular?

I am trying to change vm.info when it is coming back from an API call (using an outside library) and then display this in my view. 我正在尝试从API调用返回vm.info时(使用外部库),然后在我的视图中显示它。 But for some reason this is not updating my view and I can't use functions bound on my controller inside the function vm.session.service("ALMemory").done(function (ALMemory) 但是由于某种原因,这不会更新我的视图,因此我无法在vm.session.service("ALMemory").done(function (ALMemory)

Controller: 控制器:

app.controller('cogController',
  function () {
        var vm = this;

        vm.testFunc = function() {
          alert('test');
        }
        // Get a response from API call
        vm.session.service("ALMemory").done(function (ALMemory) {
          ALMemory.subscriber("getJSON").done(function (subscriber) {
            subscriber.signal.connect(function (msg) {
              // msg is a Json file (is defined when logging it here)
              vm.info = msg;
              // logging vm.info gives the json file
              vm.testFunc(); // This function is not called
            });
        });
  });
});

Now my vm.info is not updated in my view. 现在,我的vm.info在我看来没有更新。

View: 视图:

<body ng-app="cogApp">
 <div class="container" ng-controller="cogController as vm">
  <p>{{vm.info}}</p>
 </div>
</body>

I think you should check your services are injected and working as expected. 我认为您应该检查服务是否已注入并按预期工作。

Here is an example setting the response.data.body value to vm.info and works fine: 这是将response.data.body值设置为vm.info并且工作正常:

 angular .module('cogApp', []) .controller('cogController', ['$http', function ($http) { var vm = this; $http .get('https://jsonplaceholder.typicode.com/posts/1') .then(function(response) { vm.info = response.data.body; }); }]); 
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.10/angular.min.js"></script> <body ng-app="cogApp"> <div class="container" ng-controller="cogController as vm"> <p>{{vm.info}}</p> </div> </body> 

设置vm.info后调用$scope.$apply()

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

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