Angularjs:显示消息

时间:2016-05-28 22:37:39

标签: javascript angularjs angularjs-directive angularjs-scope compare

我尝试在密码不匹配但不显示任何想法时显示消息?我尝试在密码不匹配但不显示任何想法时显示消息?

指令:

angular.module('crud')
  .directive('equal', function () {
    return {
      require: 'ngModel',
      scope: {
        equal: '='
      },
      link: function(scope, elem, attrs, ctrl) {

        ctrl.$validators.equal = function(modelValue, viewValue) {
          return modelValue === scope.equal;
        };

        scope.$watch('equal', function(newVal, oldVal) {
          ctrl.$validate();
        });
      }
    };
  });

HTML

<form name="userForm"  ng-submit="signup()" class="form-horizontal" role="form" >
    <input type="password" class="form-control ng-invalid submitted" id="password" name="password" ng-model="password" ng-minlength="8" ng-maxlength="20"  required />

     <input type="password" equal="password" class="form-control  ng-invalid submitted" id="confirmPassword" name="confirmPassword" ng-model="confirmPassword" required  />

    <span  ng-show="userForm.password.$error.equal && userForm.password.$dirty">Passwords don't match.</span>
</form>

1 个答案:

答案 0 :(得分:0)

除了一个例外,一切都很好,你应该在密码输入上添加相同的指令,因为你显示的是密码错误,所以你应该验证密码。除此之外还可以。虽然您可能会考虑使用ng-messages来显示错误。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="@color/grey">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="match_parent"
                  android:layout_marginBottom="1dp"
                  android:padding="10dp"
                  android:background="@drawable/ripple"
                  >

        <ImageView
            android:id="@+id/listIcon"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginRight="8dp"
            android:src="@drawable/ic_launcher"
            />


        <TextView
            android:id="@+id/listTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/listIcon"
            android:textSize="16sp"
            android:textStyle="bold"
            android:text="lorum ipsum"
            android:textColor="@color/black"


            />

        <TextView
            android:id="@+id/listText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/listIcon"
            android:textSize="12sp"
            android:layout_below="@id/listTitle"
            android:text="lorum ipsum"
            />


    </RelativeLayout>
</RelativeLayout>

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:theme="@style/Theme.AppCompat.Light.DarkActionBar">


    <LinearLayout

        android:id="@+id/containerL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/trainingView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/add_wrkt_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:elevation="@dimen/fab_elevation"
        android:onClick="showAddListDialog"
        android:src="@drawable/icon_add"/>


</RelativeLayout>


    public class WorkoutPickerAdapter extends FirebaseRecyclerViewAdapter<RowInfo, WorkoutPickerAdapter.MyViewHolder> {


    private static ClickListener clickListener;

    public WorkoutPickerAdapter(Class<RowInfo> modelClass, int modelLayout, Class<MyViewHolder> viewHolderClass, Query ref) {
        super(modelClass, modelLayout, viewHolderClass, ref);
    }

    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView title;
        TextView text;
        ImageView icon;


        public MyViewHolder(View itemView) {
            super(itemView);

            title = (TextView) itemView.findViewById(R.id.listTitle);
            icon = (ImageView) itemView.findViewById(R.id.listIcon);
            text = (TextView) itemView.findViewById(R.id.listText);
            itemView.setOnClickListener(this);

        }

        @Override
        public void onClick(View view) {
            if (clickListener != null) {
                clickListener.itemClicked(view, getPosition());
            }

        }
    }

    int icons[] = {R.drawable.ex_bench_press, R.drawable.ex_incline_press, R.drawable.ex_dumbbell_flyes, R.drawable.ex_dips,
            R.drawable.ex_triceps_extension,
            R.drawable.ex_deadlift, R.drawable.ex_pull_up, R.drawable.ex_dumbbell_rows, R.drawable.ex_shrugs, R.drawable.ex_ezbar_curl,
            R.drawable.ex_hammer_curl,
            R.drawable.ex_squat, R.drawable.ex_legpress, R.drawable.ex_legcurls, R.drawable.ex_calvesraise, R.drawable.ex_military_press,
            R.drawable.ex_lateralraise, R.drawable.ex_barbell_rows, R.drawable.ez_barbell_curl,
            R.drawable.ic_launcher


    };


    @Override
    protected void populateViewHolder(MyViewHolder viewHolder, RowInfo model, int position) {
        viewHolder.title.setText(model.getTitle());
        viewHolder.text.setText(model.getText());
        String typeClean = model.getTypeClean();



    public class WorkoutPicker extends Fragment implements WorkoutPickerAdapter.ClickListener {


    private RecyclerView recyclerView;
    private FloatingActionButton fab;
    private WorkoutPickerAdapter adapter;
    private String encodedEmail;


    public WorkoutPicker() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_workout_picker, container, false);

        encodedEmail = PreferenceManager.getDefaultSharedPreferences(getActivity()).
                getString("email", null);


        Firebase workoutRecords = new Firebase("https://personalgymlog.firebaseio.com/workout").child(encodedEmail);
        adapter = new WorkoutPickerAdapter(RowInfo.class, R.layout.rv_row, WorkoutPickerAdapter.MyViewHolder.class, workoutRecords);
        adapter.setClickListener(this);


        LinearLayoutManager mLayoutManager;
        mLayoutManager = new LinearLayoutManager(getActivity());
        mLayoutManager.setReverseLayout(true);
        mLayoutManager.setStackFromEnd(true);


        recyclerView = (RecyclerView) v.findViewById(R.id.trainingView);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(adapter);
        setUpItemTouchHelper();


        fab = (FloatingActionButton) v.findViewById(R.id.add_wrkt_fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAddListDialog();
            }
        });

        return v;

    }

    private void setUpItemTouchHelper() {
        ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
                int position = viewHolder.getAdapterPosition();
                String listId = adapter.getRef(position).getKey();
                Firebase listToRemoveRef = new Firebase("https://personalgymlog.firebaseio.com/workout").child(encodedEmail).child(listId);
                Firebase listToRemoveRef2 = new Firebase("https://personalgymlog.firebaseio.com/exercises").child(encodedEmail).child(listId);
                listToRemoveRef.removeValue();
                listToRemoveRef2.removeValue();
            }

            @Override
            public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                if (viewHolder.getAdapterPosition() == 0) return 0;
                return super.getSwipeDirs(recyclerView, viewHolder);
            }
        };

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
        itemTouchHelper.attachToRecyclerView(recyclerView);
    }


    @Override
    public void onResume() {
        super.onResume();
        adapter.setClickListener(this);
    }
angular.module('crud', ['ngMessages']);

angular.module('crud')
  .directive('equal', function () {
    return {
      require: 'ngModel',
      scope: {
        equal: '='
      },
      link: function(scope, elem, attrs, ctrl) {
        ctrl.$validators.equal = function(modelValue, viewValue) {
          return modelValue === scope.equal;
        };

        scope.$watch('equal', function(newVal, oldVal) {
          ctrl.$validate();
        });
      }
    };
  });
.valid {
  border: 1px solid green;
}

.invalid {
  border: 1px solid red;
}