AngularJS 一遍又一遍地调用 http get 请求!!
如果我直接在控制器中调用 getContexts 很好......但如果我从 调用它html template
,它只会继续调用 get。
部分上下文Ctrl.js:
'use strict';
app.controller('ContextCtrl', function ContextCtrl($scope, $location, $http, $rootScope, ContextService) {
$scope.getContexts = function(keys)
{
$scope.contexts = {};
$http.get('/getContextsWS?keys=' + keys).
success(function(data){
$scope.contexts = data;
});
}
// This works
$scope.getContexts('["context::one", "context::two"]')
});
部分html部分:
<section id="contextSection" ng-controller="ContextCtrl">
// This causes get to keep calling!
{{getContexts('["context::one", "context::two"]')}}
// Just checking
{{contexts|json}}
<nav>
<ul ng-show="contexts.length" ng-cloak>
<li ng-repeat="context in contexts">
<div class="view" ng-click="contextSelected(context)">{{context.name}}</div>
</li>
</ul>
</nav>
</section>
这让我发疯!
好的,我已经尝试了您的隐藏字段示例,但我似乎无法让它工作,但也许我错过了一些东西......
的HTML:
<section id="contextSection" ng-controller="ContextCtrl">
<input type="hidden" ng-model="contextIds" copy-to-model value='@product.item.contextIds'/>
指令:
mto.directive('copyToModel', function ($parse) {
return function (scope, element, attrs) {
$parse(attrs.ngModel).assign(scope, attrs.value);
}
});
控制器:
app.controller('ContextCtrl', function ContextCtrl($scope, $location, $http, $rootScope, ContextService) {
// I can read this, but it never changes, and without this the contextIds in the alert are null
$scope.contextIds = "...";
$rootScope.alert = function(text)
{
alert(text);
}
$rootScope.alert("$scope.contextIds: " + $scope.contextIds);
});
知道我在做什么错吗?