我只是在构建一个简单的应用程序来学习AngularJS,并且在切换 View 时无法更新变量。这是我到目前为止的内容:

function routeConfig($stateProvider, $urlRouterProvider) {
$stateProvider
  .state('home', {
    url: '/',
    templateUrl: 'app/main/main.html',
    controller: 'MainController',
    controllerAs: 'main'
  })

  .state('team', {
    url: '/team',
    templateUrl: 'app/main/team.html',
    controller: 'MainController',
    controllerAs: 'main'
  })
$urlRouterProvider.otherwise('/');
}

这是我的 Controller 的一部分:
function MainController($timeout, webDevTec, toastr, $resource, $scope) {
var vm = this;

var GetTeam = $resource('https://apisite.com/api_endpoint/:teamId', {teamId: '@id'});
vm.teamName = '';
function getTeamInfo(id) {
  var teamObj = GetTeam.get({teamId: id});
    $timeout(function(){
      vm.teamName = teamObj["name"];
    },100)
};

vm.getTeamInfo = getTeamInfo;
}

然后在main.html中,通过ng-click调用getTeamInfo:
 <ul class="list-group">
<li class="list-group-item" ng-repeat="team in main.teams" ng-click="main.getTeamInfo(team.id)"><a href="#/team">{{ team.name }}</a></li>
 </ul>

单击该链接将带您进入team.html:
<div class="row">
            <div class="col-sm-12">
                <h3>{{ main.teamName }}</h3>
                <ul class="list-group">
                  . . .
                </ul>
            </div>
        </div>

由于某些原因,“main.teamName”未更新。我也尝试过$scope.$apply(function(){vm.teamName = teamObj["name"]}方法,但是没有运气。我还做了console.log(teamObj["name"])之前的'vm.teamName'和之后的'console.log(vm.teamName)',以查看是否获得了预期的结果并且可以。我现在不知道为什么不更新新 View 。

感谢您的见识,耐心和时间!

更新1

我也尝试在变量($ scope.teamName)上使用$ scope,并在运气不好的情况下使用$ scope。$ apply(function(){$ scope.teamName = teamObj [“name”]})。

更新2

我也尝试过调用$ scope。$ apply();。在'vm.teamName = teamObj [“name”]'之后,没有运气

最佳答案

分配teamObj时,似乎尚未填充vm.teamName
如果仅引用teamObj而不是创建新属性,您将使生活变得更加轻松。

我根据代码的修改版本制作了一个插件,以显示可能的实现。我无法使用 Controller 使用语法来使它正常工作,我不确定原因(可能是由于与共享 Controller 有关的某些问题;不确定)。无论如何,希望它将对您有所帮助。

DEMO

app.js

var app = angular.module('plunker', ['ui.router', 'ngResource']);

app.controller('MainController', MainController);
app.config(routeConfig);

function MainController($timeout, $scope, $resource) {

  // mock data
  var GetTeam = $resource('http://demo7592070.mockable.io/teams/:teamId', {teamId: '@id'});
  //var GetTeam = $resource('https://apisite.com/api_endpoint/:teamId', {teamId: '@id'});

  $scope.teamName    = 'undefined';
  $scope.getTeamInfo = getTeamInfo;

  function getTeamInfo(id) {

    var teamObj = GetTeam.get({teamId: id});

    $scope.teamName = teamObj.name;
    $scope.teamObj = teamObj;

  };

}

function routeConfig($stateProvider, $urlRouterProvider) {

$stateProvider
  .state('home', {
    url: '/',
    templateUrl: 'main.html',
    controller: 'MainController'
  })

  .state('team', {
    url: '/team',
    templateUrl: 'team.html',
    controller: 'MainController'
  });

  console.log('ROUTECONFIG');

  $urlRouterProvider.otherwise('/');

}

index.html
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />

       <!-- JS (load angular, ui-router, and our custom js file) -->
    <script src="http://code.angularjs.org/1.2.13/angular.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.2/angular-resource.js"></script>
    <script src="app.js"></script>

  </head>

  <body>
    <a ui-sref="home">home</a>
    <a ui-sref="team">team</a>
    <div ui-view=""></div>
  </body>

</html>

main.html
<h1>Home</h1>

<pre>$scope.teamName        => {{teamName}}</pre>
<pre>$scope.teamObj         => {{teamObj}}</pre>
<pre>$scope.teamObj.name    => {{teamObj.name}}</pre>

<button ng-click="getTeamInfo(1)">Get Team 1</button>

team.html
<h1>Team</h1>

<pre>$scope.teamName        => {{teamName}}</pre>
<pre>$scope.teamObj         => {{teamObj}}</pre>
<pre>$scope.teamObj.name    => {{teamObj.name}}</pre>

<button ng-click="getTeamInfo(2)">Get Team 2</button>

关于angularjs - 切换 Angular View 不更新变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31397993/

10-11 18:21