這裏有兩個例子:
使用NG-包括和指向外部文件
我創建了一個plunker showing you a working example的功能。只要你點擊'流程數據'鏈接,它就會調用NewBox()來添加外部文件中的內容。這個鏈接模擬你的回調。
在該指令中,模板被定義爲模板:
<div ng-include="getTemplateUrl()"></div>
和鏈接功能,我安裝getTemplateUrl()一次newBox()被調用的... getTemplateUrl()函數返回的名稱外部文件(例如template.html)的:
link: function(scope, element, attr) {
scope.newBox = function() {
console.log('new Box');
scope.getTemplateUrl = function() {
return 'template.html';
}
}
}
完整JS文件是:
angular.module('components', [])
.directive('newHost', function() {
return {
restrict: 'E',
template: '<div ng-include="getTemplateUrl()"></div>',
link: function(scope, element, attr) {
scope.newBox = function() {
console.log('new Box');
scope.getTemplateUrl = function() {
return 'template.html';
}
}
}
}
});
angular.module('HelloApp', ['components'])
.controller('MyCtrl', ['$scope', function($scope) {
$scope.name = 'This is the controller';
$scope.go = function() {
console.log('Processing...');
$scope.newBox();
}
}]);
index.html的是:
<!doctype html>
<html ng-app="HelloApp" >
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-controller="MyCtrl">
<new-host></new-host>
<br/>
<a ng-href='#here' ng-click='go()'>process data</a>
</div>
</body>
</html>
而且template.html是一個簡單的例子:
<div>
This is some content from template.html
</div>
如果您在plunker看,一旦你按下 '過程數據' 中,template.html含量則使用newBox()函數添加。現在,你會用你的回調替換那個鏈接。
使用NG秀和一個布爾隱藏/顯示內容這是在一個稍微不同的方向前進
一種方式比你是用NG-顯示和隱藏模板,直到newBox()是調用。我建立了JSFiddle that shows an example of how to do this。
新主機的代碼使用NG-顯示在開始隱藏:
<div ng-controller="MyCtrl">
<new-host ng-show='displayNewHost'></new-host>
<br/>
<a ng-href='#here' ng-click='go()' >process data</a>
</div>
鏈接過程數據來模擬你的成功回調,所以當你點擊它,它會調用$範圍。newBox()
這裏是主要的JS文件:
angular.module('components',[])
.directive('newHost', function() {
return {
restrict: 'E',
link: function(scope, element, attr) {
scope.newBox = function() {
console.log('new Box');
scope.displayNewHost = true;
};
}
}
})
angular.module('HelloApp', ['components'])
function MyCtrl($scope) {
$scope.displayNewHost = false;
$scope.name = 'This is the controller';
$scope.go = function() {
console.log('Processing...');
$scope.newBox();
}
}
angular.module('myApp', ['components'])
正如你看到的,在控制器中,我們設置displayNewHost爲false隱藏指令。一旦點擊過程數據鏈接,newBox函數將displayNewHost設置爲true,然後顯示內容。
在你的例子中,你將'templateUrl'替換爲'template'指向你的文件。
這是另一種解決方案。
編輯我的答案來回答後續的問題/ newBox不是一個函數錯誤
只是讀你的代碼(不使用plunker所以我可能是錯的檢查),我猜測,這個問題是一旦你在成功函數$ scope中指向另一個範圍。試着將你的代碼改成這個......請注意,我把$ scope放在一個變量'vm'中,然後在覈心函數和成功回調函數中使用它。
host.controller('hostsController', function($scope, $http, $window, $rootScope){
$rootScope.$on("$routeChangeError", function (event, current, previous, rejection) {
console.log("failed to change routes");
});
var vm = $scope;
vm.newHost = {};
vm.addNewHost = function() {
$http({
method : 'POST',
url : 'http://192.168.0.99:5000/newHost',
data : JSON.stringify(vm.newHost), // pass in data as strings
})
.success(function(data) {
console.log(data);
vm.newBox()
//on success we want to close the modal and reset the data
vm.newHost = {}
vm.dismiss()
});
};
});'
使用'templateUrl'屬性設置路徑模板 – charlietfl
templateUrl必須是同一級別的鏈接,你有內部函數裏面的語法無效。動態模板可以用ng-include完成我認爲 – YOU