为什么在 AngularJS 中无法重用服务刷新视图
Why won't re-usable service refresh view in AngularJS
AngularJS中的html视图包含一个表单,该表单通过视图的控制器将其数据发送到可重用的服务中。 可重用服务接收表单的值,并负责更新两个 Cookie 的值,这两个 Cookie 的值应打印在视图中。 在我的开发框中,FireFox 调试器显示表单的数据进入 serice 的处理程序方法。 但是视图不会使用由服务的表单处理程序更改的 cookie 的值进行更新。 具体而言,应根据表单处理程序更改的 Cookie 的值显示或隐藏视图元素。 需要对下面的代码进行哪些具体更改,以便表单处理程序更改的 cookie 值能够更改视图中显示的内容?
重新创建此问题所需的所有代码都在 plnkr 中,您可以通过单击此链接进行检查,并且您可以通过按照 gui 进行/someroute
(从下拉导航菜单(并在表单中输入值来重现问题。
可重用服务someclass.js
,其内容如下:
angular
.module('someclass', ['ngCookies'])
.service('someclass', ['$rootScope', '$http', '$cookies', function($rootScope, $http, $cookies){
var $this = this;
this.prop1 = $cookies['test1'];
this.prop2 = $cookies['test2'];
this.resultphone = {};
this.method1 = function(isValid, resultphone) {
if(isValid){
var funcJSON = $this.resultphone;
funcJSON.wleadid = '1';
$cookies.test1 = 'yes';
if(funcJSON.phonenum1=='ok'){
$cookies.test2 = 'ok';
} else {
$cookies.test2 = 'notok';
}
}
};
}]);
html 视图是someroute.html
的,其内容是:
someclass.prop1 is: {{someclass.prop1}} <br>
someclass.prop2 is: {{someclass.prop2}} <br>
<div ng-show="someclass.prop1!='yes'">
<h1>someclass prop1!</h1>
<div ng-include="'someroute_start.html'"></div>
</div>
<div ng-show="someclass.prop1=='yes'">
Inside prop1 is yes. <br>
<div ng-show="someclass.prop2=='ok'">
<h1>Include start. ok. </h1>
<div ng-include="'someroute_first.html'"></div>
</div>
<div ng-show="someclass.prop2!='ok'">
<h2>Include second. NOT ok. </h2>
<div ng-include="'someroute_second.html'"></div>
</div>
</div>
上面提到的someroute_start.html
Web 表单是:
<form name="confirmForm" ng-submit="someclass.method1(confirmForm.$valid)" novalidate>
Enter some value:
<input type="text" name="phonenum1" ng-model="someclass.resultphone.phonenum1" required />
<button type="submit" ng-disabled="confirmForm.$invalid" >Submit</button>
</form>
someroute.html
的控制器是someroute.js
的,其代码是:
angular
.module('someroute', ['someclass'])
.controller('someroute', function($scope, someclass) {
$scope.someclass = someclass;
});
重现问题所需的所有剩余代码都在 plnkr 中,您可以通过单击此链接进行检查。 需要对 plnkr 中的代码进行哪些具体更改才能根据someclass.js
服务对视图表单数据的处理来刷新/更改someroute.html
视图?
请注意,您可以通过单击 GUI 中的注销按钮来清除 cookie 并重新启动测试。
使用 $interval
检查 cookie 值应该适合您。
但是,您的代码中还存在其他问题:
1. nagivation controller is not used, hence, logout doesn't work
2. $cookie API of version 1.3+ is used while you are using angular 1.2
这里的这个 plunker 将它们全部固定下来。
您需要隔离关注点...使用 $cookie 更新 Cookie 值,使用 $this 更新作用域值。这不是一个$watch问题,只是因为"prop1"和"prop2"忽略了cookie内容:
this.method1 = function(isValid) {
if(isValid){
var funcJSON = $this.resultphone;
funcJSON.wleadid = '1';
$cookies.test1 = 'yes';
if(funcJSON.phonenum1=='ok'){
$cookies.test2 = 'ok';
$this.prop2 = 'ok';
} else {
$cookies.test2 = 'notok';
$this.prop2 = 'notok';
}
}
};
我做了一个更新的 plunker
- 如何使用WCF服务和javascript表单post上传.doc文件
- 如何使 Angular 服务中的数据通过页面刷新持久化
- 角度:如何刷新服务数据
- 如何在AngularJS中刷新时在服务中持久保存数据
- 如何在刷新时将持久数据保存在角度路由服务中
- 为什么在 AngularJS 中无法重用服务刷新视图
- 如何使用 Angular 刷新页面以从服务中获取新值
- 在使用angularjs刷新页面时,不应删除服务数据
- 如何刷新列表以从服务获取新数据
- 使用AngularJS服务和视图处理页面刷新
- angular http获取请求通过服务需要刷新以显示数据
- AngularJS通过服务传递数据,但在页面刷新时丢失数据
- 如何使用asp.net web服务来刷新asp.net 4.0网页的一小部分,而无需编辑任何其他部分
- 使用$http刷新来自服务的数据时,角度视图不更新
- Chrome浏览器在angular web应用中刷新grunt服务时出错
- d3.js日小时热图加载服务器重启,但消失时,页面刷新
- 我的服务不会立即显示变化.必须刷新浏览器
- 从具有初始数据的服务角度刷新范围
- Angularjs通过$http刷新视图.插入数据库后获取服务
- 在angularjs中,认证服务对象在刷新时会丢失页面