标签: angularjs-scope

Angular Directive刷新参数更改

我有一个角度指令,初始化如下:

<conversation style="height:300px" type="convo" type-id="{{some_prop}}"></conversation>
Run Code Online (Sandbox Code Playgroud)

我希望它足够聪明,可以在$scope.some_prop更改时刷新指令,因为这意味着它应该显示完全不同的内容.

我已经测试了它,没有任何反应,连接函数甚至在$scope.some_prop更改时都没有被调用.有没有办法让这种情况发生?

javascript angularjs angularjs-directive angularjs-scope

72
推荐指数
2
解决办法
12万
查看次数

如何在angularjs ui-router中的状态之间共享$ scope数据?

如果不在父控制器中使用服务或构建观察者,那么如何让儿童状态访问主控制器$scope.

  .state("main", {
      controller:'mainController',
      url:"/main",
      templateUrl: "main_init.html"
  })  
  .state("main.1", {
      controller:'mainController',
      parent: 'main',
      url:"/1",
      templateUrl: 'form_1.html'
  })  
  .state("main.2", {
      controller:'mainController',
      parent: 'main',
      url: "/2",
      templateUrl: 'form_2.html'
  })  
Run Code Online (Sandbox Code Playgroud)

我无法在子状态下访问mainController范围 - 或者说我正在获取该范围的另一个实例 - 而不是我想要的.我觉得我错过了一些简单的事情.状态对象中有一个共享数据配置选项,但我不确定是否应将此类用于此类.

javascript angularjs angularjs-scope angular-ui-router

71
推荐指数
4
解决办法
9万
查看次数

简单的ng - 包括不工作

我是第一次玩AngularJS,我努力使用ng-include作为我的页眉和页脚.

这是我的树:

myApp
assets
   - CSS
   - js
        - controllers
        - vendor
             - angular.js
             - route.js
             ......
             ......
             ......
        main.js
pages
   - partials
        - structure
              header.html
              navigation.html
              footer.html
   index.html
   home.html
Run Code Online (Sandbox Code Playgroud)

index.html的:

<!DOCTYPE html>
<html ng-app="app">
<head>
    <title>AngularJS Test</title>

    <script src="/assets/js/vendor/angular.js"></script>
    <script src="/assets/js/vendor/route.js"></script>
    <script src="/assets/js/vendor/resource.js"></script>
    <script src="/assets/js/main.js"></script>

</head>
    <body>          

        <div ng-include src="partials/structure/header.url"></div>
        <div ng-include src="partials/structure/navigation.url"></div>    

        <div id="view" ng-view></div>   

        <div ng-include src="partials/structure/footer.url"></div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

main.js

var app = angular.module("app", ["ngResource", "ngRoute"]);


app.config(function($routeProvider) {

$routeProvider.when("/login", {
    templateUrl: "login.html",
    controller: "LoginController"
});

$routeProvider.when("/home", { …
Run Code Online (Sandbox Code Playgroud)

html javascript angularjs angularjs-scope

68
推荐指数
3
解决办法
9万
查看次数

AngularJS指令不会更新范围变量更改

我试图编写一个小指令,用其他模板文件包装其内容.

这段代码:

<layout name="Default">My cool content</layout>
Run Code Online (Sandbox Code Playgroud)

应该有这个输出:

<div class="layoutDefault">My cool content</div>
Run Code Online (Sandbox Code Playgroud)

因为布局"默认"具有以下代码:

<div class="layoutDefault">{{content}}</div>
Run Code Online (Sandbox Code Playgroud)

这里是指令的代码:

app.directive('layout', function($http, $compile){
return {
    restrict: 'E',
    link: function(scope, element, attributes) {
        var layoutName = (angular.isDefined(attributes.name)) ? attributes.name : 'Default';
        $http.get(scope.constants.pathLayouts + layoutName + '.html')
            .success(function(layout){
                var regexp = /^([\s\S]*?){{content}}([\s\S]*)$/g;
                var result = regexp.exec(layout);

                var templateWithLayout = result[1] + element.html() + result[2];
                element.html($compile(templateWithLayout)(scope));
            });
    }
}
Run Code Online (Sandbox Code Playgroud)

});

我的问题:

当我在模板中使用范围变量时(在布局模板中或布局标签内),例如.{{whatever}}它刚开始工作.如果我更新whatever变量,则指令不再更新.整个链接功能只会触发一次.

我认为,AngularJS不知道,该指令使用范围变量,因此不会更新.但我不知道如何解决这个问题.

javascript angularjs angularjs-directive angularjs-scope

66
推荐指数
4
解决办法
14万
查看次数

ng-model不适用于AngularJS中的单选按钮

我是Angular的新手,我正在尝试获取用户使用ng-model选择的单选按钮的值.但我没有在"选定的联系人"中获得任何输出.

这是我的HTML

<!doctype html>
<html ng-app>
  <head>
    <script src="http://code.angularjs.org/1.2.0rc1/angular.min.js"></script>
    <script src="script.js"></script>
  </head>
  <body>
    <form name="myForm" ng-controller="Ctrl">
      <table border="0">
                <th>Contact Type</th>
                <tr ng-repeat="contact in contacttype"><td>
                <input type="radio" ng-model="contactname" name="group1" value="{{contact.name}}">{{contact.name}}                  
                </td>               
            </td></tr></table>
      <tt>selected contact = {{contactname}}</tt><br/>
     </form>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

下面是我的main.js

  function Ctrl($scope) {
  $scope.contacttype = [ 
                          {name: 'Both' }, 
                          {name: 'User'},
                          {name: 'Admin'}
                          ];
}
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?无法搞清楚!!!

javascript angularjs angularjs-scope

64
推荐指数
2
解决办法
4万
查看次数

窗口内部宽度更改的AngularJS事件

我正在寻找一种方法来观察窗口内部宽度变化的变化.我尝试了以下内容,但没有奏效:

$scope.$watch('window.innerWidth', function() {
     console.log(window.innerWidth);
});
Run Code Online (Sandbox Code Playgroud)

有什么建议?

javascript jquery angularjs angularjs-scope

63
推荐指数
4
解决办法
11万
查看次数

如何使用angularjs检测浏览器?

我是angularjs的新手.如何在angularjs中检测userAgent.是否可以在控制器中使用它?试过类似下面的东西,但没有运气!

  var browserVersion = int((/msie (\d+)/.exec(lowercase(navigator.userAgent)) || [])[1]);
Run Code Online (Sandbox Code Playgroud)

我需要专门检测IE9!

angularjs angular-ui angularjs-directive angularjs-scope

63
推荐指数
6
解决办法
13万
查看次数

AngularJS指令动态模板

我正在尝试使用基于范围值的differtent模板制作指令.

这是我到目前为止所做的,我不知道为什么不起作用http://jsbin.com/mibeyotu/1/edit

HTML元素:

<data-type content-attr="test1"></data-type>
Run Code Online (Sandbox Code Playgroud)

指示:

var app = angular.module('myApp', []);

app.directive('dataType', function ($compile) {

    var testTemplate1 = '<h1>Test1</h1>';
    var testTemplate2 = '<h1>Test2</h1>';
    var testTemplate3 = '<h1>Test3</h1>';

    var getTemplate = function(contentType){

        var template = '';

        switch(contentType){
            case 'test1':
                template = testTemplate1;
                break;
            case 'test2':
                template = testTemplate2;
                break;
            case 'test3':
                template = testTemplate3;
                break;
        }

        return template;
    }; 

    var linker = function(scope, element, attrs){
        element.html(getTemplate(scope.content)).show();
        $compile(element.contents())(scope);
    };

    return {
        restrict: "E",
        replace: true,
        link: linker,
        scope: {
            content:'='
        } …
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-directive angularjs-scope

63
推荐指数
4
解决办法
6万
查看次数

AngularJS ngRepeat元素删除

关于如何在ngRepeat指令中实现项目删除有很多问题,正如我所知,它归结为使用ngClick并触发一些删除函数传递项目的$ index.

但是,我找不到任何有多个ngRepeats的示例:

<div ng-controller="MyController">
    <div ng-repeat="email in user.emails">
        {{ email }} <a href>Remove</a>
    </div>

    <div ng-repeat="phone in user.phones">
        {{ phone }} <a href>Remove</a>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

为此,我需要创建$ scope.removePhone$ scope.removeEmail,这将使用ngClick on Remove anchor来调用.但我正在寻找更通用的解决方案.特别是因为我有很多页面有很多ngRepeats.

我正在考虑编写一个可以放在Remove锚点上的指令,并且会做类似这样的事情:

  1. 在父元素中查找ngRepeat.
  2. 阅读它的迭代内容(第一种情况为'user.emails',第二种情况为'user.phones')
  3. THAT模型中删除$ index元素.

所以标记看起来像这样:

<div ng-controller="MyController">
    <div ng-repeat="email in user.emails">
        {{ email }} <a href remove-directive="$index">Remove</a>
    </div>

    <div ng-repeat="phone in user.phones">
        {{ phone }} <a href …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angularjs-directive angularjs-scope

56
推荐指数
3
解决办法
7万
查看次数

$ watch未在阵列更改时触发

我想弄清楚为什么我$watch没有被触发.这是相关控制器的片段:

$scope.$watch('tasks', function (newValue, oldValue) {
    //do some stuff
    //only enters here once
    //newValue and oldValue are equal at that point
});

$scope.tasks = tasksService.tasks();

$scope.addTask = function (taskCreationString) {
    tasksService.addTask(taskCreationString);//modifies tasks array
};
Run Code Online (Sandbox Code Playgroud)

在我看来,tasks显然正在更新正确,因为我的长度限制如下:

<span>There are {{tasks.length}} total tasks</span>
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

angularjs angularjs-scope

55
推荐指数
3
解决办法
5万
查看次数