我正在尝试在从API调用(使用外部库)返回时更改vm.info,然后在我的视图中显示它。但由于某种原因,这不是更新我的视图,我不能在函数<a href="/images/image-name.jpg" download="new-image-name.jpg">
控制器:
vm.session.service("ALMemory").done(function (ALMemory)
现在我的视图中没有更新我的vm.info。
查看:
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
});
});
});
});
答案 0 :(得分:1)
我认为您应该检查您的服务是否按预期注入并正常工作。
以下是将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;
});
}]);
&#13;
<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>
&#13;
答案 1 :(得分:-1)
在设置vm.info
后调用$scope.$apply()