Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angularjs获取模板并将变量传递给模板 {{foo}} 点击我 var-app=angular.module(“app”,[]); app.controller('ctrl',函数($scope,$http){ $scope.bar=123; $scope.myFun=函数(){ $http.get('template.html')。然后(函数(响应){ $scope.foo=response.data; }); }; });_Javascript_Angularjs - Fatal编程技术网

Javascript angularjs获取模板并将变量传递给模板 {{foo}} 点击我 var-app=angular.module(“app”,[]); app.controller('ctrl',函数($scope,$http){ $scope.bar=123; $scope.myFun=函数(){ $http.get('template.html')。然后(函数(响应){ $scope.foo=response.data; }); }; });

Javascript angularjs获取模板并将变量传递给模板 {{foo}} 点击我 var-app=angular.module(“app”,[]); app.controller('ctrl',函数($scope,$http){ $scope.bar=123; $scope.myFun=函数(){ $http.get('template.html')。然后(函数(响应){ $scope.foo=response.data; }); }; });,javascript,angularjs,Javascript,Angularjs,//模板 <section ng-app="app" ng-controller="ctrl"> <div id="output">{{ foo }}</div> <button ng-click="myFun()">Click me</button> </section> var app = angular.module("app", []); app.controller('ctrl', func

//模板

<section ng-app="app" ng-controller="ctrl">   
    <div id="output">{{ foo }}</div>
    <button ng-click="myFun()">Click me</button>
</section>

var app = angular.module("app", []);
app.controller('ctrl', function($scope, $http){
    $scope.bar = 123;
    $scope.myFun = function(){
        $http.get('template.html').then(function(response){
            $scope.foo = response.data;
        }); 
    };  
});
{{bar}
我是新来的

我尝试创建一个ng点击,并像ajax一样将模板数据导入页面

我尝试在模板内部使用变量

有人知道如何在angularjs中将变量传递给模板吗

我想用另一种方法来解决你的问题,希望这个例子能对你有所帮助


样品
点击我
var-app=angular.module(“app”,[]);
应用程序控制器(“ctrl”,
职能($范围){
$scope.bar=123;
$scope.myFun=函数(){
$scope.template=“temp.html”;
};
});

模板

 <html ng-app="app" ng-controller="ctrl">
 <head>
<title>sample</title>
</head>
<body>
<div ng-include="template"></div>
<button ng-click="myFun()">Click me</button>

<script src="angular.js"></script>
<script type="text/javascript">
    var app = angular.module("app", []);
    app.controller("ctrl",
        function ($scope) {
            $scope.bar = 123;

            $scope.myFun = function () {
                $scope.template = "temp.html";
            };
        });
</script>
{{bar}

您能解释一下原因吗?如果我想附加数据,而不是替换数据,该怎么办?因为在angular中,我们使用[指令、服务、工厂和过滤器]来处理这种情况。请阅读更多关于[ngInclude]的内容,我相信您可以找到完成项目的答案。在angular中,您不需要像jQuery那样处理代码。注意:您也可以使用嵌套的ngInclude。
 <html ng-app="app" ng-controller="ctrl">
 <head>
<title>sample</title>
</head>
<body>
<div ng-include="template"></div>
<button ng-click="myFun()">Click me</button>

<script src="angular.js"></script>
<script type="text/javascript">
    var app = angular.module("app", []);
    app.controller("ctrl",
        function ($scope) {
            $scope.bar = 123;

            $scope.myFun = function () {
                $scope.template = "temp.html";
            };
        });
</script>
<h1>{{ bar }}</h1>