您不能绑定变量。但是,您可以绑定包含此变量的变量访问器或对象。这是固定的jsfiddle。
基本上,您必须将可以返回/或保留当前值的内容传递给范围。例如
厂:
app.factory('testFactory', function(){
var countF = 1;
return {
getCount : function () {
return countF;
},
incrementCount:function(){
countF++;
return countF;
}
}
});
控制器:
function FactoryCtrl($scope, testService, testFactory)
{
$scope.countFactory = testFactory.getCount;
$scope.clickF = function () {
$scope.countF = testFactory.incrementCount();
};
}
视图:
<div ng-controller="FactoryCtrl">
<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>