0

我对 AngularJS 相当陌生,所以我认为这将是关于混淆范围的事情。

当我从 ng-click 锚链接将新项目推送到服务支持模型时,我正在尝试获取一个 ng-repeat 列表来监视范围内的服务以进行更新。

一个失败的小提琴示例在这里:http: //jsfiddle.net/znFwY/

JS:

'use strict';

angular.module('myApp', [])
    .factory('Order', function () {
        return {
            packages: {
                theList: [],
                list: function () {
                    return this.theList;
                },
                push: function (p) {
                    if (p === undefined || !!this.theList[p.title]) return;
                    this.theList[p.title] = p;
                    console.log('pushed ' + p.title);
                },
                contains: function (p) {
                    return  !!this.theList[p.title];
                },
                print: function () {
                    console.log(this.theList);
                }
            }
        }
    })
    .controller('AppCtrl', function ($scope, $http, $location, Order) {
        $scope.order = Order;
        $scope.data = {
            packages: [
                {title: 'one'},
                {title: 'two'},
                {title: 'three'},
            ]
        };
    })
;

HTML:

<div ng-app="myApp">
    <div ng-controller="AppCtrl">

        <ul id="package-list">
            <li ng-repeat="package in data.packages | orderBy:package.order">
                <a ng-click="order.packages.push(package)" ng-class="{selected: order.packages.contains(package)}">{{package.title}}</a>
            </li>
        </ul>

        <ul id="basket-list">
            <li ng-repeat="package in order.packages.list() | orderBy:package.order"> {{package.title}}</li>
        </ul>

        <a ng-click="order.packages.print()">print list</a>
    </div>
</div>

谢谢

4

0 回答 0