【发布时间】:2017-05-02 23:49:56
【问题描述】:
您好,我正在尝试创建一个组件,它在控制器中工作正常,但不能将值绑定到视图。
我的组件如下
app.component("bdObjects", {
templateUrl: "app/templates/components/BusinessObjects.html",
controller: ["$scope", "$http", "$log", "API_ROOT", "VisDataSet",
function ($scope, $http, $log, API_ROOT, VisDataSet) {
$scope.fnAdd = function() {
$scope.objectId = "";
$scope.objectName = "Test Object";
console.log($scope.objectName);
}
$scope.cancelAdd = function() {
if($scope.items.length > 0) {
$log.info($scope.items[0]);
$scope.fnPopulateForm($scope.items[0]);
}
}
}],
bindings: {
data: "=",
objectId: "=",
objectName: "="
}
});
我的模板
<div class="general-form">
<input type="hidden" name="id" ng-model="objectId">
<label>Name:</label>
<br>
<input class="form-control" name="name" placeholder="Name" ng-model="objectName">
<br>
</div>
所以在添加按钮上,我尝试为输入框赋值。但它没有采取。我想让这两种方式绑定。所以稍后我会有保存按钮,所以更改 TextBox 中的值将在 Controller 中替换。
谢谢。
【问题讨论】:
-
bindings将值绑定到控制器实例,而不是$scope。 -
在
cancelAdd你有$scope.items。items是否应该是data绑定?您应该提供组件的使用方式。
标签: angularjs binding components two-way