0
私はAngularJSを初めて使い、親スコープの値をバインディングのないディレクティブスコープ(2つの別々のインスタンス)にコピーする方法を知りました。私のnewHostTemplateは{{newHost.ipAddress}}
を呼び出して実装されていますが、newHostはディレクティブのスコープであり、親ではありません。親スコープからディレクティブスコープへの値のコピー
host.directive('newHost', function(){
return {
restrict: 'E',
template: '<div ng-include="getTemplateUrl()"></div>',
scope: true,
link: function(scope, element, attr) {
scope.newBox = function() {
scope.getTemplateUrl = function() {
return 'hosts/newHostTemplate.html';
}
}
}
}
});
host.controller('hostsController', function($scope, $http, $window, $rootScope){
$rootScope.$on("$routeChangeError", function (event, current, previous, rejection) {
console.log("failed to change routes");
});
$scope.newHost = {};
$scope.addNewHost = function() {
$http({
method : 'POST',
url : 'http://192.168.0.99:5000/newHost',
data : JSON.stringify($scope.newHost), // pass in data as strings
})
.success(function(data) {
console.log(data);
$scope.newBox()
$scope.newHost = {}
//on success we want to close the modal and reset the data
$scope.dismiss()
});
};
});
現在、私がこれを実行すると、newBoxが関数ではないというエラーが表示されます。
こんにちはアルテム、上記の範囲は、私のシナリオに適用される – DorkMonstuh
あなたのスコープ入力がスコープになりますか:{ホスト:「=」}、あなたがスコープに渡します.new-hostから –
テンプレートを使用する代わりに静的テンプレートURLのテンプレートをng-includeにしているのはなぜですか: '' hosts/newHostTemplate.html '' ?これにより、ng-includeによって作成される追加のスコープが作成されなくなります。テンプレートは、ディレクティブのスコープ内に直接存在します。 –