我有一个控制器来获取table1中的所有记录。在table2中,我有一个table1的外键。使用machineController,我设法在html中填充select标签。但是想在post上填充sparePartController。由于某种原因这不起作用,我在某个地方犯了一个错误,你能帮我理解一下吗?
<div ng-app="app" ng-controller="sparePartController as vm">
<div class="form-group" ng-controller="machineController as machineVM">
<label>Machine Type</label>
<select class="form-control" ng-model="vm.newSparepart.machine" ng-options="machine.name for machine in machineVM.machines"></select>
</div>
</div>
//sparePartController.js
(function () {
"use strict";
angular.module("app")
.controller("sparePartController", sparePartController);
function sparePartController($http)
{
var vm = this;
vm.spareParts = [];
vm.newSparePart = {};
vm.errorMessage = "";
vm.isBusy = true;
$http.get("/spares/getAll")
.then(function (response) {
//success
angular.copy(response.data, vm.spareParts);
}, function (error) {
vm.errorMessage = error;
}).finally(function () {
vm.isBusy = false;
});
vm.addSparePart = function () {
vm.isBusy = true;
vm.errorMessage = "";
$http.post("/spares", vm.newSparePart)
.then(function (response) {
alert("Test");
vm.spareParts.push(response.data);
vm.newSparePart = {};
}, function () {
alert(vm.data);
vm.errorMessage = "failed to save new spare";
}).finally(function () {
vm.isBusy = false;
});
};
}
})();
// machineController.js
(function () {
"use strict";
angular.module("app")
.controller("machineController", machineController);
function machineController($http) {
/* jshint validthis:true */
var vm = this;
vm.machines = [];
vm.newMachine = {};
vm.errorMessage = "";
vm.isBusy = true;
$http.get("/machines/GetMachines")
.then(function (response) {
// Success
angular.copy(response.data, vm.machines);
}, function (error) {
// Failure
vm.errorMessage = "Failed: " + error;
}).finally(function () {
vm.isBusy = false;
});
vm.addMachine = function () {
vm.isBusy = true;
vm.errorMessage = "";
$http.post("/machines", vm.newMachine)
.then(function (response) {
vm.machines.push(response.data);
vm.newMachine = {};
}, function () {
//fail
vm.errorMessage = "Failed to save new machine type";
}).finally(function () {
vm.isBusy = false;
});
};
}
})();
请你检查一下,我哪里错了? Code to check
答案 0 :(得分:1)
Angular中的范围使用原型继承,即所有父属性都可供儿童访问。当试图访问子范围内的属性时,解释器遍历从子节点开始的原型链并向上移动父链而不是相反,即您希望在父范围内保持子属性。
简单来说。您无法从父母那里访问子范围。
那么这个问题的解决方案是什么。
在父作用域中定义属性并在子
使用service / rootscope(不推荐)在控制器之间共享数据
使用compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
或$emit
选择使用哪种技术完全取决于您所使用的应用程序类型。所以明智地选择。
下面是一个使用服务帮助您在两个控制器之间共享数据的示例
$broadcast
&#13;
var myApp = angular.module('myApp', []);
myApp.service('Data', function() {
this.message = "I'm data from a service";
this.secondMessage = "Another Data in service";
})
function FirstCtrl($scope, Data) {
$scope.data = Data;
}
function SecondCtrl($scope, Data) {
$scope.data = Data;
}
function thirdCtrl($scope, Data) {
$scope.data = Data;
}
&#13;
Reference : Discussion on Share data from child to parent controller
In Depth Scope Inheritance in Angular
希望这会有所帮助:)
答案 1 :(得分:0)
请申请此代码,它会起作用。
var testCtrl1ViewModel = $scope.$new();
$controller('childControllerName', {$scope: testCtrl1ViewModel});