我希望我在 Controller 中的模型不是像 Number
、Boolean
、String
、Object 这样的普通对象
,Array
,而是一个由function-constructor创建的对象。
在 AngularJS 中可以吗?
Here是我的 html:
<body ng-app="MyApp" ng-controller="MyController">
<input ng-model="model.text" type="text"/>
</body>
和我的脚本:
angular.module('MyApp',[]).
constant('MyModel',function(){
var MyModel = function(text){
this.text = text;
};
return MyModel;
}).
controller('MyController',
[
'$scope',
'MyModel',
function(
$scope,
MyModel
){
//Does not work
$scope.model = new MyModel('dummy text');
//Works
//$scope.model = {text:'dummy text'};
}])
最佳答案
常量
不需要包装在函数中...
angular.module('MyApp',[]).
constant('MyModel',function(text){
this.text = text;
}).
// ...
关于javascript - 如何绑定(bind)由 AngularJS 中的函数构造函数创建的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22525393/