javascript - 如何绑定(bind)由 AngularJS 中的函数构造函数创建的对象

标签 javascript angularjs data-binding model angularjs-scope

我希望我在 Controller 中的模型不是像 NumberBooleanStringObject 这样的普通对象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;
}).

// ...

http://jsfiddle.net/S5VXv/

关于javascript - 如何绑定(bind)由 AngularJS 中的函数构造函数创建的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22525393/

相关文章:

android - 数据绑定(bind)是否将 UI 逻辑移出了布局?

wpf - 如何使用 MVVM 在更改绑定(bind)到的属性时淡出数据绑定(bind)文本 block

php - 防止类为 'sub-menu' 的 ul 元素成为 Wordpress 菜单中列表项的子元素

javascript - 如何从 iFrame 内部重定向?

javascript - angular.js 和 html5 如何使用三元运算符?

javascript - 在部分功能之后重新渲染

c# - 将 WPF 控件绑定(bind)到 Timer 中更新的数据是否安全?

javascript - 在图像 map 上的鼠标悬停时使用音频

javascript - 为什么我无法从 angular.js 工厂获取对象 firebase?

javascript - 如何在 jsFiddle 中设置 Angular 示例应用程序?