(1) 用ui-router定义路由,比如有两个页面,一个页面(producers.html)放置了多个producers,点击其中一个目标,页面跳转到对应的producer页面,同时将producerId这个参数传过去。
})
.state('producers'<span style="color: #000000">,{
url: '/producer/:producerId'<span style="color: #000000">, templateUrl: 'views/producer.html'<span style="color: #000000">, controller: 'ProducerCtrl'<span style="color: #000000">
})
(2)在producer.html中,定义点击事件,比如ng-click="toProducer(producerId)",在ProducersCtrl中,定义页面跳转函数(使用ui-router的$state.go接口):
(3)在ProducerCtrl中,通过ui-router的$stateParams获取参数producerId,
<span style="color: #0000ff">var producerId =<span style="color: #000000"> $stateParams.producerId;
});
举例:有N个页面,每个页面都需要用户填选信息,最终引导用户至尾页提交,同时后一个页面要显示前面所有页面填写的信息。这时用factory传参是比较合理的选择
3、基于factory和$rootScope.$broadcast()的传参
(1)举例:
</span><span style="color: #008000">//</span><span style="color: #008000"> 定义components数组,数组<a href="https://www.jb51.cc/tag/baokuo/" target="_blank" class="keywords">包括</a>街道,城市,国家等</span>
address.components =<span style="color: #000000"> [];
</span><span style="color: #008000">//</span><span style="color: #008000"> 定义更新地址<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>,通过$rootS<a href="https://www.jb51.cc/tag/cop/" target="_blank" class="keywords">cop</a>e.$<a href="https://www.jb51.cc/tag/bro/" target="_blank" class="keywords">bro</a>adcast()设置全局事件'AddressUpdated'</span>
<span style="color: #008000">//</span><span style="color: #008000"> 所有子作用域都能监听到该事件</span>
address.updateAddress = <span style="color: #0000ff">function</span><span style="color: #000000"> (value) {
</span><span style="color: #0000ff">this</span>.components =<span style="color: #000000"> angular.<a href="https://www.jb51.cc/tag/cop/" target="_blank" class="keywords">cop</a>y(value);
$rootS<a href="https://www.jb51.cc/tag/cop/" target="_blank" class="keywords">cop</a>e.$<a href="https://www.jb51.cc/tag/bro/" target="_blank" class="keywords">bro</a>adcast(</span>'AddressUpdated'<span style="color: #000000">);
};
</span><span style="color: #008000">//</span><span style="color: #008000"> 返回地址对象</span>
<span style="color: #0000ff">return</span><span style="color: #000000"> address;
}]);
(2)在获取地址的controller中:
$scope.components =<span style="color: #000000"> [];
$scope.$watch('components',<span style="color: #0000ff">function<span style="color: #000000"> () {
<span style="color: #008000">//<span style="color: #008000"> 将component对象推入$scope.components数组
<span style="color: #000000"> components.push(component);
<span style="color: #008000">//<span style="color: #008000"> 更新addressFactory中的components
<span style="color: #000000"> addressFactory.updateAddress(components);
});
(3)在监听地址变化的controller中:
shopFactory.getShops(street,city).then(<span style="color: #0000ff">function<span style="color: #000000"> (data) {
<span style="color: #0000ff">if(data.status === 200<span style="color: #000000">){
$scope.shops =<span style="color: #000000"> data.shops;
}<span style="color: #0000ff">else<span style="color: #000000">{
$log.error('对不起,获取该位置周边商铺数据出错: '<span style="color: #000000">,data);
}
});
});
4. 基于localStorage或sessionStorage的页面跳转传参
//<span style="color: #008000"> 可以用于更新参数counter
counterFactory.updateCounter().then(<span style="color: #0000ff">function<span style="color: #000000"> (data) {
<span style="color: #008000">//<span style="color: #008000"> 将新的counter值上传到localStorage中
$scope.$storage.counter =<span style="color: #000000"> data.counter;
});
(2)监听localStorage中的参数变化 - Controller B
5. 基于localStorage/sessionStorage和Factory的页面传参