相关疑难解决方法(0)

AngularJs"控制器为"语法 - 澄清?

读到了关于 angularJS 的新语法controller as xxx

语法InvoiceController as invoice告诉Angular实例化控制器并将其保存在当前范围的变量发票中.

可视化:

在此输入图像描述

好的,所以$scope我的控制器中没有参数,控制器中的代码会更清晰.

我将不得不在视图中指定另一个别名

所以到现在为止我能做到:

<input type="number" ng-model="qty"  />

....controller('InvoiceController', function($scope) {
   // do something with $scope.qty <--notice
Run Code Online (Sandbox Code Playgroud)

现在我可以这样做:

 <input type="number" ng-model="invoic.qty"  /> <-- notice 

  ....controller('InvoiceController', function() {
       // do something with  this.qty  <--notice
Run Code Online (Sandbox Code Playgroud)

这样做的目标是什么?从一个地方删除并添加到另一个地方?

我很高兴看到我错过了什么.

javascript angularjs

119
推荐指数
4
解决办法
7万
查看次数

Angular.js"Controller as ..."+ $ scope.$ on

如果我想在Angular中使用"Controller as ..."语法,我应该如何处理$ scope($)这样的东西,我需要放入控制器?

我得到一个印象,我可以做一些其他方式,如下所示.在这里,要获得$ scope.$ on work i将"this"绑定到回调函数.我试图在控制器内的"this"上调用$ on但它没有用.

你能不能在这里给我一个提示,或者如果我完全搞砸了,你能指点我做一些正确的方法吗?谢谢.

main.js:

angular.module('ramaApp')
.controller('MainCtrl', ['$scope', '$location', function ($scope, $location) {

    this.whereAmINow = 'INDEX';

    $scope.$on('$locationChangeStart', function(event) {

        this.whereAmINow = $location.path();

    }.bind(this));

    this.jumpTo = function(where) { $location.path(where); }
}]);
Run Code Online (Sandbox Code Playgroud)

index.html的:

<div ng-controller="MainCtrl as main">

    <p>I am seeing the slide named: {{ main.whereAmINow }}</p>

    <div ng-click="main.jumpTo('/slide1')">Slide 1</div>
    <div ng-click="main.jumpTo('/slide2')">Slide 2</div>
    <div ng-click="main.jumpTo('/slide3')">Slide 3</div>

</div>
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-scope

13
推荐指数
1
解决办法
5049
查看次数

标签 统计

angularjs ×2

angularjs-scope ×1

javascript ×1