如何访问controllerAs中的控制器变量?

时间:2017-04-25 10:44:55

标签: angularjs angularjs-controlleras

我用自耕农启动应用程序,他们使用" controllerAs"启动app.js ..

我知道通过这种方式我不会使用 $ scope ,但我必须使用这个来访问控制器属性,但在某种程度上我和#39;我无法想象这些变化......有人可以帮助我吗?

我在这里做了什么:

app.js

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slide_button_wrapper left first" ><p class="text">Text1</p></div>
<div class="slide_button_wrapper right first" ><p class="text">Text2</p></div>
<div class="slide_button_wrapper left second"><p class="text">Text3</p></div>
<div class="slide_button_wrapper right second" ><p class="text">Text4</p></div>

<div class="slide_button_wrapper left third" ><p class="text">Text5</p></div>

控制器(主要)

            angular
              .module('viaggiApp', [
                'ngAnimate',
                'ngCookies',
                'ngResource',
                'ngRoute',
                'ngTouch',
                'ngMaps'
              ])
              .config(function ($routeProvider, $locationProvider) {
                $routeProvider
                  .when('/', {
                    templateUrl: 'views/main.html',
                    controller: 'MainCtrl',
                    controllerAs: 'main'    
                  })
                  .when('/about', {
                    templateUrl: 'views/about.html',
                    controller: 'AboutCtrl',
                    controllerAs: 'about'
                  })
                  .otherwise({
                    redirectTo: '/'
                  });
                   $locationProvider.hashPrefix('');      
              });

HTML

 angular.module('viaggiApp').controller('MainCtrl', function ($q,GetPathGoogle,NgMap) {
this.propriety="say hi";
});

为什么HTML不能正确呈现适当值,但它会显示括号?

2 个答案:

答案 0 :(得分:1)

试试这个

<html>
<head>
	<script Src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>

	<script>
		var app=angular.module("myapp", []);
		app.controller("namesctrl", function(){
            this.propriety="say hi";
		});
		
	</script>


</head>
<body ng-app="myapp" ng-controller="namesctrl as ctrl">
{{ctrl.propriety}}
</body>
</html>

答案 1 :(得分:0)

这应该有效,

<div ng-controller="MainCtrl as main">
   <h1> {{main.propriety}} </h1>
</div>
相关问题