【发布时间】:2014-12-29 14:40:02
【问题描述】:
我想在 ng-init 中初始化数据,想在控制器中检索这些数据。
我有一个带有表单的视图。在这种形式中,我有以下输入:
<input class="form-control" type="text" id="name" name="name"
ng-model="type.name" required="true"
placeholder="Enter type name..." ng-init="reftypedefinition.name='{{type.name}}'"/>
在 Chrome 开发者工具中,我看到了 reftypedefiniton name 的值:
并在我的控制器中注入 $window 服务:
$scope.createType = function () { $scope.reftypedefinition.name = $window.reftypedefinition.name;
};
当我运行时出现错误:
TypeError: Cannot read property 'name' of undefined
at Scope.RefTypeDetailsController.$scope.createType (http://localhost:8080/...js:74:63)
at Parser.functionCall (http://localhost:8080/.../js/lib/angular.js:10846:21)
at ngEventDirectives.(anonymous function).compile.element.on.callback (http://localhost:8080/.../js/lib/angular.js:19133:17)
at Scope.$get.Scope.$eval (http://localhost:8080/ism-server/js/lib/angular.js:12701:28)
at Scope.$get.Scope.$apply (http://localhost:8080/ism-server/js/lib/angular.js:12799:23)
at HTMLButtonElement.<anonymous> (http://localhost:8080/.../js/lib/angular.js:19138:23)
at HTMLButtonElement.x.event.dispatch (http://localhost:8080/.../js/lib/jquery.min.js:5:10006)
at HTMLButtonElement.x.event.add.y.handle (http://localhost:8080/**/js/lib/jquery.min.js:5:6796)
怎么了?为什么名称在控制器上不可用? 感谢您的任何建议
【问题讨论】: