【发布时间】:2014-08-19 22:54:57
【问题描述】:
我是 AngularJS 新手,我的第一个项目有问题: 如果我更新一个特殊的 $scope-variable,则不会应用更改。更改另一个 $scope-variable 工作正常。我尝试使用 $scope.$apply 但它说“$apply 已经在进行中”。使用 ng-inspector(一个 Chrome 扩展程序)我发现新值被分配给 $scope-variable 但这个更改并没有被 ng-repeat 应用...... 这是代码(咖啡脚本)。有问题的变量名为“ingreds”:
angular.module 'magicPeda', ['ngDragDrop']
.controller 'IngredientsCtrl', ($scope, $http) ->
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"
base = null
$http.get('/base').then (iResponse) ->
base = iResponse.data
$scope.ingreds = base
$scope.combination = []
$scope.cook = ->
ids = []
for ingreds in ($scope.combination)
ids.push ingreds._id
ids = JSON.stringify ids
IdString = $.param({ingredids: ids})
request = $http({
method: "POST",
url: "/cook",
data: IdString,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then (response) ->
base.push response.data
$scope.ingreds = base
$scope.combination = []
$scope.reset = ->
$scope.ingreds = base
$scope.combination = []
和html部分:
<div class="container" ng-controller="IngredientsCtrl">
<table class="table" >
<tr ng-repeat="ingredient in ingreds">
<td>
<img ng-src="/images/{{ingredient.pic}}" data-drag="true" ng-model="ingredient"
-jqyoui-options="{revert: 'invalid'}" jqyoui-draggable="{animate:true}" ng-hide="!ingredient.name"></img>
{{ingredient.name}}
</td>
</tr>
</table>
<div class="thumbnail" data-drop="true" ng-model="combination" jqyoui-droppable="{multiple:true}" style='height:150px; bottom: 0px;'>
<div class="btn-group">
<button type="button" class="btn btn-danger" style="height: 100%" ng-click="reset()" ng-show="combination[0].name">Reset</button>
<button type="button" class="btn btn-default" ng-repeat="item in combination track by $index">
<img ng-show="item.name" data-drag="false" data-jqyoui-options="{revert: 'invalid', helper: 'clone'}" ng-model="combination" jqyoui-draggable="{index: {{$index}}, placeholder: 'keep'}" ng-src="/images/{{item.pic}}" />
</button>
<button type="button" class="btn btn-success" style="height: 100%" ng-click="cook()">Cook!</button>
</div>
</div>
</div>
【问题讨论】:
-
你可以尝试使用 $timeout(function () { });而是
标签: javascript angularjs