您不能绑定变量。但是您可以绑定包含此变量的变量访问器或对象。这是固定的jsfiddle。
基本上,您必须将可以返回/或保留当前值的内容传递给范围。例如
厂:
app.factory('testFactory', function(){ var countF = 1; return { getCount : function () { return countF; //we need some way to access actual variable value }, incrementCount:function(){countF++; return countF; } } });
控制器:
function FactoryCtrl($scope, testService, testFactory){ $scope.countFactory = testFactory.getCount; //passing getter to the view $scope.clickF = function () { $scope.countF = testFactory.incrementCount(); };}
视图:
<div ng-controller="FactoryCtrl"> <!-- this is now updated, note how count factory is called --> <p> This is my countFactory variable : {{countFactory()}}</p> <p> This is my updated after click variable : {{countF}}</p> <button ng-click="clickF()" >Factory ++ </button></div>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)