更新范围变量不会更改输出

时间:2016-07-11 10:13:36

标签: angularjs jhipster

显然,我在之前的解释中并不清楚,所以我们走了:

function HomeController ($scope, Principal, LoginService, NewtagService, LoginfailService, $state, $http) {

    ...      
    $scope.contacts = [{name: 'Peter'}];
    ...
}

<div ng-controller="HomeController">{{contacts[0].name}}</div> 

这会打印'Peter',而

function HomeController ($scope, Principal, LoginService, NewtagService, LoginfailService, $state, $http) {

    // $scope.contacts = [{name: 'Peter'}];

    function createServiceTag () {

        $http({
            method: 'POST',
            url: '/api/important'
        }).then(function successCallback(response) {

            $scope.contacts = [
                {name:'Lois'}
            ];
            ImportantService.open(response.data);
        }, function errorCallback(response) {
            console.log(response);
        });
    }
}

<div ng-controller="HomeController">{{contacts[0].name}}</div> 

什么都不打印

2 个答案:

答案 0 :(得分:1)

function makeImportantRequest () {

        $http({
            method: 'POST',
            url: '/api/important'
        }).then(function successCallback(response) {
                      $scope.contacts = [
                    {name:'Luise'}
                ];


                ImportantModalService.open();

        }, function errorCallback(response) {

        });
    }

答案 1 :(得分:1)

在没有$(function(){})

的情况下尝试
  $http({
            method: 'POST',
            url: '/api/important'
        }).then(function successCallback(response) {

             $scope.contacts = [
                    {name:'Luise'}
                ];
                ImportantModalService.open();

        }, function errorCallback(response) {

        });
相关问题