我有一个应用程序,在主页上我显示所有的伙伴(从使用GET的JSON)在页面上。我有一个按钮可以在主页上添加好友。 点击后,它会将我带入不同的视图,其中包含表单字段以输入好友详细信息。 提交详细信息后,我再次被带到应该向其他好友呈现新加入好友的主页。从控制器更改视图不触发视图更新
一切工作正常。但唯一的问题是在提交好友的详细信息后返回主页,页面并未反映新添加的好友的视图。但范围正在更新。
我试过使用$ scope。$ apply()和$ scope。$ digest(),这里提到的是angular.js: model update doesn't trigger view update但它并没有更新我的视图。此外,我使用相同的控制器为我的主页和添加好友,以便我的范围相同。
有人可以告诉我我在做什么错吗? 有没有其他办法可以实现?
我的代码在这里。
Home.html中视图
<div class="jumbotron">
<h1>Welcome to My Buddy List</h1>
<a class="btn btn-primary btn-lg" href="#/add_buddy">Add Buddy</a>
</div>
<h2>My Buddies</h2>
<ul class="media-list">
<li class="media col-md-6" ng-repeat="buddy in buddies|filter:search">
<a class="pull-left thumbnail" href="#/buddy/{{buddy.id}}">
<img class="media-object" width="100" ng-rc="img/{{buddy.image_name}}.jpg" alt="{{buddy.username}}">
</a>
<div class="media-body">
<h4 class="media-heading">{{buddy.username | uppercase}}
<span class="badge">{{buddy.status}}</span>
</h4>
<p>
<strong>First Name:</strong> {{buddy.first_name | capitalize}}<br/>
<strong>Last Name:</strong> {{buddy.last_name | capitalize}}<br/>
<strong>Status:</strong> {{buddy.status | capitalize}}<br/>
<strong>Type:</strong> {{buddy.type | capitalize}} Buddy
<a class="trashcan" data-ng-click="removeBuddy(buddy.id)" ><img class="trashcan" src="img/trashcan.png" alt="Delete"</a>
</p>
</div>
</li>
添加好友视图(add_buddy.html)
<div class="panel panel-default">
<div class="panel-heading">
<small><a href="#">Go Back</a></small>
<h3>Add Buddy</h3>
</div>
<div class="panel-body">
<form name="buddyForm" class="app-form">
<label>User name</label> : <input type="text" name="userName" ng-model="user.userName" required><br/>
<label>First name</label> : <input type="text" name="firstName" ng-model="user.firstName" required><br/>
<label>Last name</label> : <input type="text" name="lastName" ng-model="user.lastName" ><br/>
<label>Email</label> : <input type="email" name="email" ng-model="user.email" ><br/>
<!--label>Comments</label> : <textarea type="textarea" name="bio" ng-model="user.bio" ></textarea><br/-->
<button class="submitButton" data-ng-click="addThisBuddy(user)">Submit</button>
</form>
</div>
</div>
控制器代码:
$scope.addThisBuddy = function() {
$scope.newBuddy = [{
id: 100,
first_name: $scope.user.firstName,
last_name: "Anand",
birthday: "05/23/1998",
starred: "true",
username: $scope.user.userName,
image_name: "anand",
type: "Hometown",
bio: "Viswanathan Anand was born on 11 December.",
last_login: "05/23/2016",
email: "[email protected]"
}];
$scope.buddies = $scope.buddies.concat($scope.newBuddy);
$location.path('home'); //skipping to view the newly added buddy on homepage
$scope.$apply(); //tried but not updating view
$scope.$digest(); //tried but not updating view
};
我的含路由app.js
angular.module('myApp', ['ngRoute'])
.config(function($routeProvider) {
$routeProvider.when('/home', {templateUrl: 'partials/home.html', controller: 'HomeController'});
$routeProvider.when('/buddy/:id', {templateUrl: 'partials/buddy.html', controller: 'BuddyController'});
$routeProvider.when('/type/:type', {templateUrl: 'partials/buddy_type.html', controller: 'BuddyTypeController'});
$routeProvider.when('/starred', {templateUrl: 'partials/starred_buddy.html', controller: 'StarredController'});
$routeProvider.when('/add_buddy', {templateUrl: 'partials/add_buddy.html', controller: 'HomeController'});
$routeProvider.otherwise({redirectTo: '/home'});
});
编辑注:
做更多的搜索后,我发现,改变使用$位置的观点是重装控制器。因此,范围得到更新并恢复到其以前的状态。 有什么办法可以绕过吗?
我对这两个视图使用相同的控制器。我的$ scope.addThisBuddy函数位于HomeController中。如果我使用console.log,我可以查看更新后的Buddylist,但视图没有更新。 – Taani
我认为这是$ location.path;它会重置所有控制器。 (http://stackoverflow.com/questions/14485117/angularjs-location-path-change-without-all-controllers-resetting)你可以尝试做console.log _after_ $ location.path代码? –
我在$ location.path后执行了console.log。它正在与新朋友更新范围。尽管视图没有更新。 – Taani