猿问

AngularJS-在页面之间传递数据

我是AngularJS入门者。我正在尝试从发送数据:


A页:范列表页面



网页B:Van更新页面。

当用户单击货车的更新链接时,我正在调用控制器并在控制器中检索货车详细信息。但是,我无法使用同一控制器将货车详细信息分配给页面B(货车更新页面)...错误"Cannot set property 'vanNumber' of undefined"


*** Page A: Van List ****


<form name="listVanForm" >

   <table>

   <tr> <td ng-controller="VanUpdateCtrl"><a href="#/van-update" ng-click="prePopulateForm(row.members.vanNumber.value )" class="btn btn-small btn-primary">update</a></td> </tr>

   </table>

</form>


*** Page B: Van Update ****


  <div class="container">

        <h2>Edit Van </h2>


        <form name="updateVanForm" novalidate="novalidate" class="form-horizontal" ng-submit="updateCard(formData)">

            <div class="control-group">

                <label class="control-label" >Van Number:</label>


                <div class="controls">

                    <input type="text" id="vanNumber" ng-model="formData.vanNumber" placeholder=""/>

                </div>

            </div>

        </form>

     </div>


*** VanUpdateCtrl **


   app.controller('VanUpdateCtrl', ['$scope', 'VanUpdateFactory', '$location',

                                      function ($scope, VanUpdateFactory, $location) {


        //callback for ng-init 'populateDD':    

        $scope.prePopulateForm = function (cardNoParam m) {


            alert('cardNo = '+cardNoParam);


            $scope.formData.cardNumber=cardNoParam;}

    }


So, $scope.formData.cardNumber OR $scope.formData in the destination page is not recognised.


长风秋雁
浏览 868回答 3
3回答

慕尼黑5688855

您需要创建一个服务,以便能够在控制器之间共享数据。app.factory('myService', function() {&nbsp;var savedData = {}&nbsp;function set(data) {&nbsp; &nbsp;savedData = data;&nbsp;}&nbsp;function get() {&nbsp; return savedData;&nbsp;}&nbsp;return {&nbsp; set: set,&nbsp; get: get&nbsp;}});在控制器A中:myService.set(yourSharedData);在控制器B中:$scope.desiredLocation = myService.get();请记住通过将myService作为参数传递到控制器中。

慕标琳琳

如果只需要在视图/作用域/控制器之间共享数据,最简单的方法是将其存储在$ rootScope中。但是,如果需要共享功能,最好定义一个服务来实现。

幕布斯7119047

app.factory('persistObject', function () {&nbsp; &nbsp; &nbsp; &nbsp; var persistObject = [];&nbsp; &nbsp; &nbsp; &nbsp; function set(objectName, data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; persistObject[objectName] = data;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function get(objectName) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return persistObject[objectName];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set: set,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get: get&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });像这样填充数据persistObject.set('objectName', data);&nbsp;像这样获取对象数据persistObject.get('objectName');&nbsp;
随时随地看视频慕课网APP

相关分类

AngularJS
我要回答