简体   繁体   English

作用域变量未传递到使用ng-include包含的模板

[英]scope variable not getting passed to template included using ng-include

Scope variable not getting passed to template included through ng-include tag. 作用域变量未传递到通过ng-include标签包含的模板。 I want the generic.html page to come with page1.html and page2.html. 我希望generic.html页面随附page1.html和page2.html。 I am getting the static content Hi,Welcome like that but when I click a method in controller1.js the name enclosed in angular directive is not getting printed. 我正在获取静态内容嗨,欢迎这样,但是当我在controller1.js中单击一个方法时,没有打印出包含在angular指令中的名称。 I tried to name in one object like $scope.user = {} and added user.name then also I am not able to bond name in the html template. 我试图用$ scope.user = {}之类的对象命名,并添加了user.name,但我也无法在html模板中绑定名称。

Main.js Main.js

 $stateProvider
        .state('index', {
            parent: 'app',
            abstract : true,
            url: '/index',
            views: {
                'content@': {
                    templateUrl: urlpath+'ngtemplates/chat/chat.menu.html',
                    controller: ['$state',function ($state) {
                      $state.go('screen.welcome');
                    }]
                }
            }
        })
        .state('screen.welcome', {
            url: '',
                    templateUrl: urlpath+'templates/page1.html',
                    controller:'Controller1'
        })
        .state('screen.details', {
            url: '',
                    templateUrl: urlpath+'templates/page2.html',
                    controller:'Controller2'
        })

Controller1.js Controller1.js

$scope.selected = function(){
$scope.name = "Jim";
}

generic.html generic.html

<div ng-app="sampleapp">
<h1> Hi, Welcome! </h1>
<p> {{name}} </p>
</div>

You need to add controller to the template 您需要将控制器添加到模板

   <div ng-app="sampleapp">
     <div ng-controller = "Controller1"> //controller added 
        <h1> Hi, Welcome! </h1>
        <p> {{name}} </p>
    </div>
        </div>

You can also use controllerAs syntax. 您还可以使用controllerAs语法。

.state('screen.details', {
        url: '',
                templateUrl: urlpath+'templates/page2.html',
                controllerAs:'vm'
    })

And in the Html you can access like {{vm.name}}. 在HTML中,您可以像{{vm.name}}一样进行访问。

Point here is if you use ControllerAs syntax you no need to put ng-controller in your HTML. 这里要指出的是,如果您使用ControllerAs语法,则无需在HTML中放入ng-controller。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM