AngularJS array.push ng :repeat not updating

标签 angularjs

我在服务中有一个创建函数,它负责获取输入数据、将其转换为资源、进行保存并将其插入本地缓存。

create = (data, success, error) ->
    throw new Error("Unable to create new user, no company id exists") unless $resource_companies.id()

    # create the resource
    new_model = new resource(data)

    # manually add the company id into the payload (TODO: company id should be inferred from the URL)
    new_model.company_id = $resource_companies.id()

    # do the save and callbacks
    new_model.$save( () ->
        list.push(new_model)
        success.call(@) if success?
    , error)

我的问题是监视列表变量的 ng:repeat 没有得到更新。据我所知,这不是在 AngularJS 外部发生的,因此它不需要 $scope.$apply() 来保持最新状态(实际上,如果我尝试触发摘要,我会得到一个已经在进行中的摘要错误)

更奇怪的是,我在其他地方使用了完全相同的模式,没有任何问题。

我的 Controller 用来访问列表数组的代码是(这个在服务中)

# refreshes the users in the system
refresh = (cb) -> list = resource.query( () -> cb.call(@) if cb? )

在 Controller 中:

$scope.users = $resource_users.refresh()

最佳答案

这可能是因为您正在分配一个新的引用。 Angular 正在观察旧的引用,它永远不会改变。这是引用被替换的地方:

list = resource.query( /* ... */ )

相反,如果您将列表设为对象的属性,angular 的 ng-repeat watch 应该能够观察到 datalist 属性的变化对象:

data.list = resource.query( /* ... */ )

关于AngularJS array.push ng :repeat not updating,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17055945/

相关文章:

angularjs - 如何使用 Controller 显示和隐藏登录链接?

javascript - 为什么我的按钮没有使用 ng-disabled 禁用?

angularjs - 在快速服务器上成功调用 api 后重定向具有 Angular 路由的页面

angularjs - 在 k-content 属性中添加剑道工具提示模板

AngularJS 检查是否按下了其中一个性别按钮

javascript - 我应该把 AngularJS 工厂和服务放在哪里?

javascript - 从 Controller 调用特定指令实例的函数

mysql - 在 Google Cloud 上部署移动网络应用

angularjs - 加载变量时运行函数

javascript - Angular 1.x 中有默认的解析器/格式化程序吗?